Mongo update query equvalent to SQL [duplicate] - mongodb

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})

Related

Expand a MongoDB record with a new field [duplicate]

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'}}
]);

Update specific field in all documents in MongoDB [duplicate]

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);

MongoDB only removing first field from the collection [duplicate]

This question already has answers here:
Why this update query only update one record once
(3 answers)
Closed 7 years ago.
I'm a beginner in MongoDb.I wanted to know what is the query to remove a field in Mongo DB Collection.I have tried with
db.Component.update({},{$unset: {vendor:""}}).
Its was updating only first document.So How to remove field Completely from Collection?
Try this-db.Component.update({},{$unset: {vendor:""}},false,true)
Here false

Mongodb - Finding a field value existence in another field [duplicate]

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.

check if one key is greater than another key in mongodb [duplicate]

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