mongoDB updateMany fields on Array - mongodb

i want to update all fields on adress , i tried $ operator and alot of other stuff that on docs , but it always fail . whats am i missing?
code :
params that function gets is

To update a position of an array you should use Dot notation.
In this case address.0 addrees.1 and so on
This is an example of the data that you provided:
https://mongoplayground.net/p/kSYLDs3jPkP
I used update but you should be able to use updateMany too

Related

How do I create an array for an updateOne() call in mongoc (C libarary for Mongodb)?

I am completely mystified (and supremely frustrated). How do I create this call using the mongoc library?
I have the following doc structure in the collection
{_id: myOID,
subsriptions: {
newProducts: true,
newBlogPosts: true,
pressReleases: true,
}
}
I want to remove one of the subscriptions, for example, the user no longer wants to receive press releases from me.
This works in the mongo shell. Now I need to do it in C code
updateOne({_id: myOID}, [{'$unset': 'subscriptions.pressReleases'}], {})
Note how the update parameter in the Mongo shell is an anonymous array. I need to do that for the bson passed in as the update parameter in the mongoc_collection_update_one() API call.
The C code for updateOne is
mongo_status = mongoc_collection_update_one (mongo_collection,
mongo_query,
mongo_update,
NULL, /* No Opts to pass in */
NULL, /* no reply wanted */
&mongo_error);
Also note that in the aggregate() API, this is done with
{"pipeline" : [{'$unset': 'elists.lunch' }] }
Neither the updateOne() shell function nor the mongoc_collection_update_one() API call accept that, they want just the array.
How do I create the bson to use as the second parameter for mongoc_collection_update_one() API call?
Joe's answer works and I am able to accomplish what I need to do.
The $unset update operator takes an object, just like $set.
updateOne({_id: myOID},{'$unset':{'subscriptions.pressReleases': true}})
OR perhaps even better
updateOne({_id: myOID},{'$unset':{'subscriptions.pressReleases': {'$exists': true}}})
which will remove the subscription flag no matter what the value is for that field.
Doing it this way does not require an anonymous array (which I still don't know how to create).

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.

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.

Mongoid, find object by searching by part of the Id?

I want to be able to search for my objects by searching for the last 4 characters of the id. How can I do that?
Book.where(_id: params[:q])
Where the param would be something like a3f4, and in this case the actual id for the object that I want to be found would be:
bc313c1f5053b66121a8a3f4
Notice the last for characters are what we searched for. How can I search for just "part" of my objects id? instead of having my user search manually by typing in the entire id?
I found in MongoDB's help docs, that I can provide a regex:
db.x.find({someId : {$regex : "123\\[456\\]"}}) // use "\\" to escape
Is there a way for me to search using the regular mongo ruby driver and not using Mongoid?
Usually, in Mongoid you can search with a regexp like you normally would with a string in your call to where() ie:
Book.where(:title => /^Alice/) # returns all books with titles starting with 'Alice'
However this doesn't work in your case, because the _id field is not stored as a string, but as an ObjectID. However, you could add (and index) a field on your models which could provide this functionality for you, which you can populate in an after_create callback.
<shameless_plug>
Alternatively, if you're just looking for a shorter solution to the default Mongoid IDs, I could suggest something like mongoid_token which makes it pretty easy to add shorter tokens/ids to your Mongoid documents.
</shameless_plug>

PHP MongoDB, update document without erasing the rest

I'm trying to update a mongo document, using PHP and the update() function. However, when I do this, it replaces the WHOLE document with the value I wanted to update. How can I fix this?
The code I have written up to now: http://twaddlr.com/view/73
(Scroll down to the "update" function. It's a database wrapper I'm writting for my site)
The key is to use $set in the update, e.g. instead of this (sorry using JavaScript syntax here, not sure about the exact PHP driver syntax):
db.my_collection.update({hello: "world"}, {foo: "bar"})
you do
db.my_collection.update({hello: "world"}, {$set: {foo: "bar"}})
If you use $set, only the properties you specify will be updated, the whole document will not be replaced.
You can read more about this in the documentation, here: http://www.mongodb.org/display/DOCS/Updating#Updating-ModifierOperations
Edit: looking at your code, this is exactly what you do in the addRow method. Just do the same thing in update.