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"] } } }])...
Related
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'm transitioning to ArangoDB for its power as a graph database and am struggling with the simple stuff.
If I have a document...
{ _id:'1234', tags:['apple','orange'] }
how can I update it to push or pull a value from the array? I would expect the syntax to be something like this, but didn't find anything in the docs...
collection.update({ _id:'1234' },{ tags:pull('mango') })
collection.update({ _id:'1234' },{ tags:push('avocado') })
Thank you!
You achieve this with AQL. For example
FOR c in collection UPDATE c WITH { tags : PUSH(c.tags, 'avocado')} IN collection
https://docs.arangodb.com/3.3/AQL/Operations/Update.html
What is the command that can update data shown in image1 to become data shown in image2,
I use update $push, but doesn't work.
Error for the command is:
Cannot apply $push modifier to non-array for field 'single'
Starts as:
I want it to be:
Documents should be selected by some field. _id field is the most common for selection. I've used _id from your screenshot.
const _id = 'z6cQHB4WauGgCBGxM';
db.collection.update({
_id: _id
}, {
$push: {
'single.hub': {
context: 222
}
}
});
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":""}}
I have a schema defined in mongoose as follow:
var VolunteerSchema = new Schema ({
......
other fields
.....
preferLocations:[{
type: Schema.ObjectId,
ref: 'Location'
}]
.....
});
I am using volunteer.save() method to update the model.
While updating to volunteer model i get the error as follow:
{ [DivergentArrayError: For your own good, using `document.save()` to update an
array which was selected using an $elemMatch projection OR populated using skip,
limit, query conditions, or exclusion of the _id field when the operation resul
ts in a $pop or $set of the entire array is not supported. The following path(s)
would have been modified unsafely:
preferLocations
Use Model.update() to update these arrays instead.]
message: 'For your own good, using `document.save()` to update an array which
was selected using an $elemMatch projection OR populated using skip, limit, quer
y conditions, or exclusion of the _id field when the operation results in a $pop
or $set of the entire array is not supported. The following path(s) would have
been modified unsafely:\n preferLocations\nUse Model.update() to update these a
rrays instead.',
name: 'DivergentArrayError' }
While updating the location I collect the _ids field in array and asigned to preferLocations as given below:
volunteer.preferLocations = locationIdsArray;
I don't get the error when I remove this line.What am I doing wrong?
When using $elemMatch in a projection, do not use document.save(). Instead, manually update your document using Model.update(). In your case you should try
volunteer.findOneAndUpdate(
{
_id: ObjectId("567452bae5b25d6e6c1a0f7e"),
localization: { '$elemMatch': { language: 'de' } }
},
{
$set: { 'localization.$.name' : "Neuer Name" }
}).exec(//...
});
click here more details