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'}}
]);
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:
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);
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:
MongoDB : aggregation framework : $match between fields
(2 answers)
Closed 8 years ago.
I want to compare one key value with another key value in mongodb. can any one help me with this query.
example:
{
"_id":"xxxxx",
"game":"xxx",
"score":100,
"hi-score":200
}
i want to check if score is greater than hi-score
Try the below query using http://docs.mongodb.org/manual/reference/operator/query/where/
db.collection.find( { $where: "this.score > this.hi-score" } );
Also, please read the warning section provided in the link.
PS: I have not tried this