I'm new with node.js and mongodb.
In my database, I have several collections (one for users, one for article, and in the future one more...).
In my server.js file, I would like to be able to write in each of these collections.
Here is the code I use, but I'm not able to access all my collections... Have you an ideas to make that possible?
var databaseUrl = "mydb"; // "username:password#example.com/mydb"
var collections = ["users", "article", "reports", "archery"]
var db = require("mongojs").connect(databaseUrl, collections);
Thank you
You don't have to know all of your collections in advance to use mongojs, you can access a collection dynamically using db.collection('name_of_collection') and use it just like an existing collection. This call will also cache it so that next time, you can say db.name_of_collection.
There's a bunch of examples at their git hub page:
https://github.com/gett/mongojs
Good luck.
Related
I created an express server for connection to my mongodb. Before I start, everything works fine.
But I dont understand some things.
I made a collection in my mongodb, projects (with an 's' at the end)
In my code I made a project.model.js like: const Project = mongoose.model("Project", ...) module.exports = Project
Elsewhere I have db.project = require("./project.model");
Everything works like a harm, creating, updating, deleting. But I dont understand. My collections in my mongodb don't have the same name. The names are Users, Materials and in my code I use user, material. Maybe it is a stupid question but how does this work?
It is the default behavior of mongoose to convert singular to plural model names.
To fix your issue & to convert your model name from plural to singular or any other name, You can write your code as shown below
const Project = mongoose.model("Project", ProjectSchema, 'Project')
module.exports = Project
Now you could see the collection name as Project.
The third parameter in the mongoose.model is the collection name.
const data: any = await db.listCollections();
I need to get only limited number of collections, you can say pagination. But I didn't find any firebase query with limit on getting collections . Because the db is design like this way that you have to get all the collections, and there is too many collections.
When using Firestore you should typically give your collections known names, so that you can repeat them in the client-side code, and generate the names for the documents.
The Firestore API isn't well suited for listing or querying collections.
I recommend pivoting, and storing the information that requires listing into documents, so that you can use the query and pagination APIs on documents.
I'm trying to finish a project to get a part-time job and I got stuck in here.
The problem is: There are 2 JSON files given for products and categories. I uploaded these JSON files using MongoDB Compass. How can I reach these 2 documents and their collections in my code? Because I need to use them to create a website. Is there a way to do this, or:
Do I have to create Schema and Model, and after that read objects from JSON files in my code, create instances and save them one by one to my database?
You can actually use the db property of your mongoose instance and issue queries directly as needed - if you need to bypass your Models for some reason.
Models and defined schema are what many find to be the benefit of using Mongoose. You don't have to use Mongoose and can use the official Nodejs driver too.
To access db directly in Mongoose:
const mongoose = require('mongoose');
mongoose.connect(`mongodb://localhost/mydb`, {useNewUrlParser: true, useUnifiedTopology: true, autoIndex: false});
const db = mongoose.connection;
db.db.collection('mycollection').find({});
In my node app, I am saving data in to dynamic mongodb collections and those collections are named after a value in the data.
Sample code:
function saveToDb(data){
let collectionName = data.someType;
let collection = getCollection(collectionName);
return collection.save(data);
}
Please note that I do not know the collection name upfront, and the collections are being created as and when the corresponding data come for the first time.
If i want to index a few known fields in this document, I am thinking of calling collection.createIndex() after every save. But, will it hit the performance too much? Or any better ways to do this?
Finally went ahead with calling collection.createIndex() after every save. Monitoring it for a few weeks now and finding no performance problem identified with ~25K records.
Note: This doesnt mean calling collection.createIndex() after every save is the ideal solution. If someone has a better one, please feel free to comment.
I need to send an array of objects with respective ID's from client-side code via JSON to an API endpoint that is serve by ExpressJS.
Now I need to update existing DB objects with all the fields from the array of objects. I suppose a for..in loop doesn't look good in this case. Neither did I find a way to update multiple documents in one run.
I use MongoJS library to work with MongoDB from NodeJS
Please advise.
may be
db.mycollection.update({name:'some name'}, {$inc:{level:1}}, {multi:true}, function() {
// the update is complete
});
for mongojs library help you...
Thanks