I am trying to update existing mongodb document using findAndModify query in node.js. I want to add product_category_ids field & remove error_state field. I want to do these two updates in single query. If I specify only $set it is working fine but if I specify both $set & $unset, it sets product_category_ids to null value.
my query is as follows:
conn.collection('error_import').findAndModify({_id:o_id},[['_id',1]],{ $set: {"product_category_ids":sss.category}}{ $unset: {"error_state":""}},{new:true},function(err,result) {
if ( err )
console.warn(err);
else {
conn.collection('product_import').insert({"tags":result.value.tags, "category_hierarchy":result.value.category_hierarchy, "error_state":result.value.error_state});
}
});
Please help me with this.Thanks in advance.
try using
{ $set: {"product_category_ids":sss.category}, $unset: {"error_state":""}}
instead of
{ $set: {"product_category_ids":sss.category}}{ $unset: {"error_state":""}}
Related
I want to update a field and array inside a mongodb collection.
myCollection.
{
position:String,
users : [String]
}
I know how to update the position using:
myCollection.updateOne({position:position})
I know how to update the users array using:
myCollection.updateOne({position:position}, { $addToSet: {users:users})
But how to update the two at the same time?
Thanks a lot guys. Backend noob here!
The first parameter of updateOne is the filter, so by running this query:
myCollection.updateOne({ position }, {
$set: { position: newPosition },
$addToSet: { users }
})
You should update the position of the document with position to newPosition and add a new users to the users array
Here is what my collection looks like
Now suppose I have to update count of 2nd document whose reportTypes.reasonId is 300. I have access to _id as well as reasonId to update the count. I am using Mongoose to query things in my Node application.
What can I try to solve this?
You can do it via arrayFilters:
db.collection.update(
{
managerId:3
},
{
$inc:{"reportTypes.$[x].count":1}
},
{
arrayFilters:[{"x.reasonId":300 }]
}
)
playground
Explained:
Specify the matching document in the query part and create arrayFilter "x" matching the correct reportTYpes array subdocument , in the update part use the $inc operation to increment the count value in the example with 1
you should use dot notation and the $ update operator to do this:
(I'm assuming your collection is called Reason)
var conditions = {
'_id': '6244........',
'reasonTypes.reasonId': 300
}
var update = {
$inc: {
'reasonTypes.$.count': 1
}
};
Reason.update(conditions, update, function(err) {
// Handle error here
})
You can find more on the operator here mongo $ update operator
I am using Mongoose in Node.js and I have a problem
I want to update a field in the document , for example, add a suffix for a filed
how do I refer to the field of found document in update action?
I try to use this. to reference the found document's fields, but it doesn't work?
what should I do?
tcModel.findOneAndUpdate({}, {title: this.title + 'x'}).then((data) => {console.log(data)})
You can use $concat in Updates with Aggregation Pipeline to do that:
tcModel.findOneAndUpdate({}, [{ $set: { title: { $concat: ["$title", "x"] } } }])...
I want to update a MongoDb collection with an Array of JSON objects.
However, I want the update to ignore any objects that already exist in the DB.
Sounds easy, but the key that allows me to know if the object exists is not the '_id', but a different 'id' (or other key).
Is that possible ?
Currently I am using it like this:
dbHandle.collection('contents').updateMany(contents);
where 'contents' is the Array of JSON objects.
Thanks in advance
The following operation updates all documents where violations are greater than 4 and $set a flag for review:
try {
db.restaurant.updateMany(
{ violations: { $gt: 4 } }, //Your Condition
{ $set: { "Review" : true } } //YOUR JSON contents
);
} catch (e) {
print(e);
}
Change the condition accordingly.
When I'm trying to update a single attribute of a user's document after the update query every attribute of the user is missing except the _id and updated attribute. What's wrong with my query?
dbwrapper.mongo.getConnection().then(function(db){
db.collection('users').update({'_id' : dbwrapper.mongo.ObjID(userID)}, {'iconID':2}, function(error, resultMongo){
console.log(error);
if(error || !resultMongo){
reject(error);
}else{
resolve(resultMongo);
}
});
});
That's how updates work with MongoDB: if the second ("update") document contains only field:value expressions, the document stored in the database will be replaced by the update document. This is documented here.
If you merely want to update the iconID field, use $set:
.update({ '_id' : ... }, { $set : { iconID : 2 }}, ...)