This question already has an answer here:
How to increment a field in mongodb?
(1 answer)
Closed 5 years ago.
What is the mongo equivalent to this sql query
UPDATE table SET field = field + 1
Trying to update a simple views counter, sql can do it in one query, can mongo do it like this as well?
See below, make sure you set the option of multi to true so that you update all records.
const table = new Mongo.Collection('table')
table.update( { }, { $inc: { field: 1 } }, { multi : true } );
Figured it out
Documents.update({"id": key},{ $inc: { views: 1 } } );
Related
This question already has answers here:
Spring Data Mongo: upsert with various fields updated
(1 answer)
Spring Data MongoDB and Bulk Update
(3 answers)
Closed 3 years ago.
Spring boot mongo repository support saveAll,but it cause unique index insert error ,so I want use update.
In my case,I want to insert or update 10 documents together.
how to use updateAll?
Though I'm not crystal clear on your question, it sounds like you need to append { multi: true } to your update query. Please see Update Multiple Documents.
Setting the multi option updates all documents found by match.
To update multiple documents, set the multi option to true. For
example, the following operation updates all documents where stock is
less than or equal to 10:
db.books.update(
{ stock: { $lte: 10 } },
{ $set: { reorder: true } },
{ multi: true }
)
you can use updateMany:
db.collections.updateMany({ query }, {"$set":{ update }}
This question already has answers here:
MongoDB atomic "findOrCreate": findOne, insert if nonexistent, but do not update
(4 answers)
Closed 3 years ago.
Is there a way to omit adding/updating document if it was found in the collection without doing two requests to Mongo server? It would be perfect if this can be done in bulk, but I would appreciate to hear other options or workarounds.
What I'm looking for is sort of opposite of upsert option:
var bulk = MyCollection.initializeUnorderedBulkOp();
bulk.find({ x: val }).upsertOnlyIfNotFound({ x: newVal });
bulk.execute();
What you are looking for is $setOnInsert.
db.MyCollection.update(
{ x: val },
{
$setOnInsert: { x: "newVal" }
},
{ upsert: true }
)
From the documentation :
If an update operation with upsert: true results in an insert of a
document, then $setOnInsert assigns the specified values to the fields
in the document. If the update operation does not result in an insert,
$setOnInsert does nothing.
This question already has answers here:
How do I perform the SQL Join equivalent in MongoDB?
(19 answers)
MongoDB Aggregate lookup in Go (mgo.v2)
(1 answer)
Closed 4 years ago.
I have two collections.
I am using MongoDB version 3.4.
Collection 1 called Results
Collection 2 called Code_Value
Here i have to update automatically the code in the result using the Code_value Collections
The results is like:
{
_id: ObjectId(xxxxxxx)
name:"Mike"
job:"3"
salary:"4"
}
{
_id: ObjectId(xxxxxxx)
name:"Joe"
job:"1"
salary:"2"
}
the Code_value is like:
{
"Job":[
{"code":"1" , "value":"IT"}
{"code":"2" , "value":"Doctor"}
{"code":"3" , "value":"Developer"}
]
"Salary":[
{"code":"1" , "value":"900000"}
{"code":"2" , "value":"100000"}
{"code":"3" , "value":"200000"}
{"code":"4" , "value":"300000"}
]
}
I'd like to have a mapping code value on the results as the following:
{
_id: ObjectId(xxxxxxx)
name:"Mike"
job:"Developer"
salary:"300000"
}
{
_id: ObjectId(xxxxxxx)
name:"Joe"
job:"IT"
salary:"100000"
}
here is the query made as function
results.find().foreach(cursor){
v=Code_value.find(cursor,code)
if(v){
results.update(job,v.value)
}
This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
Closed 6 years ago.
How would I update every record in the "title" collection for mongo, to make a field named LastReturnedName set to Name for every single record?
Thought it would be something like this:
db.title.update(
{ "LastNameReturned" : "" },
{ $set:{ "LastNameReturned" : Name } },
{ multi : true }
);
You can use foreach iteration:
db.title.find({ "LastNameReturned" : "" }).snapshot()
.forEach(function(t) {
db.title.update({_id: t._id}, {$set: {"LastNameReturned" : t.Name}});
});
NOTE: You can use snapshot() only with unsharded collections. Also you can speed up updating if you'll use bulk operation.
This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
Closed 6 years ago.
user1 in database
{
counts: [{code:"aaa", num:100}, {code:"bbb", num:200}]
}
counts is an Array, it has two elements code and num.
How to let num increase 1 if code = "aaa"?
Meteor.users.update(user1._id, {$inc: {???}}); // How to write this?
Thanks
Gotta positionally match. Example:
Players.update({
_id: Meteor.userId(),
'games._id': Session.get('gameId')
}, {
$inc: { 'games.$.score': 1 }
});