This Meteor server code needs to remove all fields except "fName" from a document found by a field and if the document does not exist then create it.
Is there a way to do that at one go? thx
myCol.update({fName: someName}, {fName: someName}); // works if doc exists, fails if no doc.
myCol.upsert({fName: someName}, {fName: someName}); // failed if doc exists, works if it exists
You can use fName :{$exists:true} in your query part.
This will update document only if fName in present.
Related
Please help, been stuck for hours, how do I delete a document that matches a query directly in pymodm.
For example:
user = User.objects.raw({'name':'Moses'})
How do I delete this user from my database collection?
you can use the delete() method to delete document from the MongoDB, try the below code this worked for me fine.
user = User.objects.raw({'name':'Moses'})
user.delete()
I'm trying to undertstand the best way to do this.
I am getting the name and email and I want to add it to my collection.
However, if the email already exists, then i don't want to insert the name and email. Is there a way to do this using upsert? I'm trying to understand from the documentation but its a bit confusing for me. http://docs.mongodb.org/manual/reference/method/db.collection.update/ Any help is greatly appreciated.
First of all, you should consider creating an unique index for email field to ensure that there could be only one document for any particular email:
db.collection.createIndex({email: 1}, {unique: true})
You could also add sparse option to allow documents without email.
Then you'll have two options depending on your particular use case: to use upsert, or to use insert ignoring duplicate key errors.
Upsert
Using the following upsert operation
db.collection.update({email: email}, {$set: {name: name}}, {upsert: true})
you will:
create new document if there is no such email yet;
update existing document with new name if the email already exists.
Here is a quotation from MondoDB documentation explaining upsert behavior when no document matches the query criteria:
The update creates a base document from the equality clauses in the <query> parameter, and then applies the update expressions from the <update> parameter.
Insert
If you don't want to update name field of an existing document, you should use basic insert operation instead:
db.u.insert({email: email, name: name})
ignoring all 11000 E11000 duplicate key errors.
I'm a MongoDB newbie, but have gone through some basic trainings. However, I'm not able to handle this (seemingly simple) situation:
I'm populating my database with results from two API calls. The first call returns a bunch of fields that include a primary key: NAME. The second call gets additional details for each NAME. I've inserted the results of the first call into the database. When I do the second call, I want to update the document with the matching NAME to include all the additional fields and values returned by the second API call. (ex. the first call returns a recipe NAME and a bunch of metadata about the recipe; the second call returns a list of ingredients, and I want to add those to the document for that recipe based on the matching NAME).
Shouldn't this be easy? Or does this constitute a merge, which I believe mongo does not support? In that case I assume I just insert the second call's results as a separate document and do future queries on NAME in order to pull both documents relating to that NAME?
You have to use db.collection.update() call to get it done. What you do in this is that you pass in the query to locate the right document, in your case it is NAME, and then update the document appropriately.
Ex: Let's say the NAME be updated is 'eamcvey' and you want to put in Address and Phone Number field. The command you would type in for that would look like:
db.collection.update(
{NAME : 'eamcvey'},
{
$set : {Address : 'Updated Value of Address'},
$set : {PhoneNumber : '123456'}
},
);
For more detailed documentation, go to this link on update command in MongoDB.
I'm handling my user register logic with Mongodb. I need insert a user if it is not exist, but get to know if it is already exists before insert, so I can notify the user he has already registered.
The update method with upsert will not return the result of how many docs have inserted ( I do not . And findAndModify method will only find docs after insert. So neither way I'm not able to know if there is already such a doc before I insert.
Is there a way to do this?
Update
update and findAndModify are not good examples. I do not want to update my doc if the user is already exists. I just want to know if the username is exists before insert. If not, then insert it.
I'm not using _id with insert. Should I use username as _id and use it to insert?
Use a unique key (on e-mail address or whatever identifies a user) and check for corresponding error code when trying to insert. That's the way. Not just for MongoDB.
db.users.createIndex({email: 1}, {unique: true})
Now when inserting a duplicate e-mail, check for error codes 11000 and 11001.
If you're willing to use _id as your userid, you can use the save() command.
This will create a new record if there's not one already, otherwise will update the existing record.
It's probably better to check for the user and then selectively update, but this is a down and dirty way of doing things:
http://docs.mongodb.org/manual/reference/method/db.collection.save/
how to update a field in a document if the field already exists else update the value of the field? $set updates fine on documents where the field already exists. just found lot of hints how to insert a new field into an entire collection but how to solve this on document basis?
Regards,
Chris
$set will add the specified field or fields if they do not exist in this document or replace the existing value of the specified field(s) if they already exist.
Use the upsert flag in your update statement.