MongoDB update deep nested field in the document - mongodb

I try to update a field in the MongoDb like this
https://mongoplayground.net/p/_jFYJGi8z46
However i get this error
fail to run update: write exception: write errors: [The array filter for identifier 'e2' was not used in the update { $set: { sizes.$[e1].wares$[e2].reserved: "2" } }]
I can't find a bug :( ... it must be a very simple one

Right... one dot was missing :( This is solved
https://mongoplayground.net/p/obdFTr6G1kj

Related

Trying to write a simple code in mongoldb, receiving an error

Just trying out MongoDB. So this might be a very basic issue. Trying to create a collection "info", but encountering errors as below. Appreciate the help.
uncaught exception: SyntaxError: missing : after property id :
#(shell):1:42
The code is as below :
db.info.insert({"userName":"John","mail”:"John#gmail.com","mobile":12345678},{"Transaction":[{"itemId":"a100","price":200},{"itemId":"a110","price":200}]},{"Payment":["Type":"Credi
t-card","Total":400,"Success":true]},{"Remarks":"1st complete record,payment successful"})
When you try, most of the developers would recommend to read the documentation of Insert data in mongodb. If you clearly understand, then you might understand the problem. The document was not well formatted which means you created object for each key:value pair. And payment is an array which holds objects. It was also not formatted.
You can try below code, and have fun with MongoDB
db.info.insert(
{
"userName":"John",
"mail":"John#gmail.com",
"mobile":12345678,
"Transaction":[{"itemId":"a100","price":200},{"itemId":"a110","price":200}],
"Payment":[{"Type":"Credit-card","Total":400,"Success":true}],
"Remarks":"1st complete record,payment successful"
}
)

Mongodb cannot use the part (...) to traverse the element ({...: undefined})]

After upgrade to 3.0 mongo driver i am receiving some new error on update request. For update like this:
db.table.update({_id: .... } , {$set : { "tags.Tag1" : true }});
I am receiving
cannot use the part (tags of tags.Tag1) to traverse the element ({tags: null})]]
The problem is that my updated document already contains default value for tags : null. If I manually remove it from document , update starts to work correctly. It is some new behavior for me , and it is happens after updating mongo driver from 2 to 3 ( not even database itself).
But now I wonder now how to avoid this error. I can of course check if "tags" already defined and only then make $set to element or the whole map. But it means 3 requests vs one old and the other problems like atomicity.
Although it's an old post but I think what you are looking for is the $ positional operator
I am guessing your "tags" is an array. So the above example could be something like
db.table.update({_id: .... } , {$set : { "tags.$.Tag1" : true }});
Hope it helps!
Yes, it can be updated... I had resolved similar problem
Resolved this
db.table.updateById({_id: .... } , {$set : { "levelSpecificData.scale.uom": "feet"}});
5b1f566ed65c7dcc34aaa7d5 MongoError: cannot use the part (scale of levelSpecificData.scale.uom) to traverse the element ({scale: false})
where in my collection 'levelSpecificData.scale' was a Boolean type T/F
I changed the default value type of levelSpecificData.scale to '{}' empty object... Surprisingly it worked fine after changing default values to object, since I want to treat scale as an object reference this solution was all good for it.

Getting Mongo write error in Meteor

I am trying to insert a new field like this, and I am getting a write error.
If I remove this line, the program works fine.
UserDetails.update({userId: Meteor.UserId()}, {$inc: {score: 5}});
Error trace is:
I20141107-12:55:38.278(5.5)? Exception in Mongo write: TypeError: object is not a function
I20141107-12:55:38.323(5.5)? at packages/mongo/mongo_driver.js:293
I20141107-12:55:38.323(5.5)? at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108)
I believe you just have a typo. It should be Meteor.userId()
Reference: https://docs.meteor.com/#/basic/Meteor-userId
UPDATE (11/9/14): It just dawned on me that you're using Meteor.userId() which gets the current user id from the Meteor.users collection. But it looks like you're trying to update the score for a user in a collection called UserDetails. The syntax to update a specific user id is this:
UserDetails.update("biwyMQCriR3KDFHod", {$inc: {score: 5} });
Where "biwyMQCriR3KDFHod" (with the double quotes) is the unique id value for that user.
I'm not sure how you're doing your update (perhaps you could share your code using http://meteorpad.com), but you might want to take a look at using Session.
Session References:
http://meteortips.com/book/sessions/
https://docs.meteor.com/#/basic/session

mongodb - i cannot update with $pushAll and simple assignment at the same time

the following fails:
db.test.update({_id:102},{$pushAll:{our_days:["sat","thurs","frid"]}, country:"XYZ"}, {upsert:true})
error message: "Invalid modifier specified: country"
The correct way seems to be:
db.test.update({_id:102},{$pushAll:{our_days:["sat","thurs","frid"]}, $set:{country:"XYZ"}}, {upsert:true})
So is it the case that I cannot mix modifiers like "$pushAll" with simple assignments like field:value, in the same update document? Instead I have to use the $set modifier for simple assignments?
Is there anything in the docs that describes this behaviour?
This happens because db.test.update({_id : 1}, {country : 1}) will just change the whole document to country = 1 and thus removing everything else.
So most probably mongo being smart tells you: You want to update specific element and at the same time to remove everything (and that element as well) to substitute it with country = 1. Most probably this is not what you want. So I would rather rise an error.
Regarding the documentation - I think that the best way is to reread mongodb update.

MongoDB update query is failing silently

I have an app using Mongoid on top of MongoDB and an update is failing silently.
The code looks like this:
# Are we getting a new attribute? Yes we are!
p "attr first name = #{#attributes['first_name']}"
if user.update_attributes!(#attributes)
u = User.find(params[:id]).to_json
end
No exception is thrown in this code. So I looked at my MongoDB log and constructed this query based on what mongo is trying to do:
db.users.update({ "_id": "4d5561276ce886c496000001" }, { $set: { "first_name": "Erinamodobo" } }, false);
Now this does not cause any exceptions but when I grab the record that was supposed to be updated with this query:
db.users.find({"email":"escharling#somecompany.com"})
I see that the "first_name" attribute has not been updated.
Any idea why this could be happening? Sounds like something stupid.
Thanks!
You need to find out what
user.update_attributes!(#attributes)
is actually doing. This could be an issue with Mongoid. When you configure Mongoid, you can set up a logger. There you should be able to see what the driver is writing to MongoDB, and that should help answer your question. The next best thing is to post your code to the Mongoid mailing list (http://groups.google.com/group/mongoid) where people who work with Mongoid all the time will probably know what's going on.
Updating Mongoid to the latest rc.7 fixed this issue
Enable "safe mode" on your operations.