Reputation: 1318
I am using MongoDB with Node for a relatively simple web application that involves users signing up and authenticating their login information. I have experience with relational DB's, but am fairly new to the NoSQL game. I am wondering if I am efficiently using the NoSQL database with the current format of my data storage?
My code as of now to save a user looks like so:
db.users.save({email: req.body.email, display_name: req.body.displayName, password: req.body.password}, function(err, saved) {
if( err || !saved )
console.log("User not saved");
else
console.log("User saved in MongoDB");
});
What is happening here is that each user is being generated his or her own document in a MongoDB "collection." I am wondering if this is going to slow things down when the user base becomes relatively large, and I need to quickly find users for authentication?
Any answer that can help me out conceptually or point me in a direction to learn more would be greatly appreciated!
Upvotes: 0
Views: 65
Reputation: 69703
In MongoDB, what's right and what's wrong depends mostly on how you are going to interact with your data. But one document per user seems like a viable strategy for most use-cases.
To speed up retrieval of users, remember to add indexes for those fields you want to retrieve them by.
In this case, the document you save has no _id
field. That means a new document will be generated with a newly generated _id, even when there is already an entry with the same values in the database. Is this what you want? When you want to use the .save(doc)
function for updating an existing document, you need to have the _id of the document with the same value as the one you want to update.
Upvotes: 1