This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
MongoDB: How to update multiple documents with a single command?
(13 answers)
Update MongoDB field using value of another field
(12 answers)
Closed 5 years ago.
I have a collection in MongoDB that contains a list of objects. Each object has an array of "updaters" which are objects with a field that I want to update. One of the updaters in that array needs a description updated. How can i update just the descrption of the the updater object, that I know "updaterId" of.
Update:
It appears to me that setting the flag multi: true will do the trick in Mongo 2.2+.
db.configurations.update(
{"updaters.updaterId": "90391154-67BB-452E-A1A7-A07A98B94F86"},
{$set: {"updaters.$.description": "This tool will prevent users from Unloading Linked Revit files for \"all users\" which causes such Linked File to be unloaded by default when opening project."}},
{multi: true}
)
You can update the description updaterId like this:
db.collection.update( {"updaters.updaterId" : updaterId } ,
{$set : {"updaters.$.description" : "My description"}} ,
false ,
true);
Related
This question already has answers here:
MongoDB - Update or Insert object in array
(10 answers)
Closed 2 years ago.
I have the following document:
{
"_id":ObjectId("idhere"),
"locations":[
{
"latitude":3453.4353,
"longitude":31.123
},
{
"latitude":1243.134,
"longitude":992.321
}
]
}
The question is:
How can I update this document locations' without overriding the entire list,just by appending a new location to the existing ones? I know that to update a document I have to use this query:
db.collection.update({ _id : ....}, {$set : { locations : ... }}) but here is the problem -> in this way I should get the existing locations before, add the new location to this array and execute the query with the updated array which would take a lot of time & resources if the locations array grows.
Any suggestions? (P.S.: I'm currently using the MongoDB in a spring-boot project and I'm using MongoRepository interface to communicate with the DB.)
I've found the solution for this question in the mongodb docs here at the "Examples" section.
A more detailed picture here:
The saving solution is to use $push instead of $set.
This question already has answers here:
Include all existing fields and add new fields to document
(6 answers)
Closed 3 years ago.
I'm trying to expand a MongoDB row with a new column/field with a new column named key_reference, which doesn't seem to work. This is my Query in Robo 3T (similar to MySQL Workbench for MySQL or PgAdmin for Postgres)
doc1=db.getCollection('store.files').find({ _id : ObjectId("5ad5a07ccbce1d0873264ee6")});
doc1({$addFileds:{'key_reference':'1234'}});
What do I miss?
$addFields is an Aggregation Pipeline Stage which means that it can be used only with .aggregate() method, try:
db.getCollection('store.files').aggregate([
{ $match: { _id : ObjectId("5ad5a07ccbce1d0873264ee6")} },
{ $addFields:{'key_reference':'1234'}}
]);
This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
Closed 6 years ago.
I need equivalent mongo query to below SQL query:
Update tableName set tableName.col1 = tableName.col2
Update: I had given incorrect solution earlier. As the answer is accepted, updating it with the right way
db.collectionName.find().snapshot().forEach(function (elem) {
db.collectionName.update({_id: elem._id}, {$set: {col2: elem.col1}});
}
);
Use normal update query along wiht $set to set new field value
db.collectionName.update({}, {$set: {col2: '$col1'}}, {multi: true})
This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 8 years ago.
Is it possible to find all documents in a collection where documents with field (_id) values exist in another field (ex. parentID). Taking in consideration that both fields exist in the documents of the same collection? Thanks
Categories.find({'_id': 'parentID'})
{
_id: 11,
parentID: 1
}
I am using MongoDB 2.6.7
Yes, this is easy to do with the $where operator.
db.Categories.find({'$where':"this._id === this.parent"})
This gives you more flexibility than your regular find syntax but be warned that MongoDB needs to evaluate the Javascript so this is slower than a regular query.
This question already has answers here:
How to update the _id of one MongoDB Document?
(7 answers)
Closed 8 years ago.
I am working on a project using MongoDB and storing users data with their email as _id field.
Now realizing that that is a stupid idea, I wanna add an extra field, let's say "userid" as the new _id field of this collection and let the email as regular field.
Is it possible to do that?
Thank you
It is not possible tp update the _id of a document.
Currently (and probably for the forseeable future) the only way to modify the _id field is to actually remove your old document and reinsert with a new _id:
// Insert our test doc
db.c.insert({_id:1,name:'sammaye'});
// Get it back out
doc=db.c.find({name:'sammaye'});
// Now lets update by removing it first
db.c.remove({_id:doc._id});
// Change the old _id to a new one
doc._id=2;
// Reinsert it and now it has new _id
db.c.insert(doc);