MongoDB ObjectID query update - node.js

Hey guys i need to write a mongodb query to update a document i,e need to increment a field in docuemnt by 1, so for that i am using $inc operation but i am facing trouble in matching the document's key with the data sent by frontend
my db looks like this
_id:ObjectID("5ed4a86126663308549f816b")
title:svsbsbsssf
description:wdwwdwd
category:Object
_id:ObjectID("5ebc1144dde4f2974e209526")
label:World after Covid
category_id:1
url:world-after-covid
image:https://*****/assets/category-1590699874832.jpg
value:1
so this category is an Object and i am getting my category id from frontend, after converting that to ObjectID, i tried to match it like
let doc = await req.db
.collection("webinars")
.updateOne({category._id: categoryID }, { $inc: { listingViews: 1 } });
but the filter inside updateOne gives me error somehow the syntax isnt correct i guess
the error is like
Unexpected token, expected "," (17:26)
15 | let doc = await req.db 16 | .collection("webinars")
17 | .updateOne({category._id: categoryID }, { $inc: { listingViews: 1 } });

There is a syntax error in the updateOne filter object, the nested field path i.e category._id should be a string, something like this
// Notice the quotes around category._id
.updateOne({ "category._id": categoryID }, { $inc: { listingViews: 1 } });

Related

How to use $inc operator for variables in MongoDB using Node.JS

I am trying to build a "number of visitors" collection in mongoDb using Node.JS backend of my website. The frontend sends the following info to Node.JS backend as JSON.
isUniqueVisitor - 1 if yes, 0 if no
country - standard country code - "JP", "IN", "UK", etc
My database looks like following
{
"today": 2019-06-07,
"uniqueVisitors": {
"count": 230,
"countries": {
"JP": 102,
"IN": 88,
"UK": 30
}
}
}
It works well if I use $inc with fixed values
Eg. $inc: {count: 1} // for string/integers keys
Eg. $inc: {"uniqueVisitors.count": 1} // inside quotes to access key of a JSON
Main issue:
I am not able to access a document name using variable.
Eg. $inc: {`uniqueVisitors.countries[${req.body.country}]`}
This creates an error as backticks can't be used for Mongo.
I tried with
Eg. $inc: {uniqueVisitors["countries"][req.body.country]}
But even this creates error.
I followed the web and found that mongo $set using variables can be realized by passing the required JSON directly to $set. Hence I resorted to code it the following way.
mongoClient.connect(mongoURL, async function (err, db) {
if (err) throw err;
console.log("Database connected");
// Identifying my document with today's date
var myQuery = {
date: getTodayDate()
};
// Defining the JSON to be passed to uniqueVisitors $inc
var uniqueVisitorsInc = {
"uniqueVisitors": {
"count": 0,
"countries": {}
}
};
// Populating the JSON to be passed to uniqueVisitors $inc => essentially asking to increase count by 1 and increase that country's count by 1
uniqueVisitorsInc["uniqueVisitors"]["count"] = 1;
uniqueVisitorsInc["uniqueVisitors"]["countries"][myData.country] = 1;
var newValues = {
$inc: uniqueVisitorsInc
};
await db.collection("visitorStats").update(myQuery, newValues, {upsert: true});
db.close();
});
The above method worked well on editor but threw the following runtime error:
$inc requires numerical values
Basically asking me to pass values to $inc in {var1: 1, var2: 5} pattern.
Please help me bypass this weird situation.
I know I can do a two step process where I read the values first, increment in variable and $set it in Mongo.
But does anyone know how to overcome this situation using $inc?
If this update were hardcoded to update "JP" only, it'd need to look like:
$inc: { "uniqueVisitors.country.JP": 1 }
So you were almost there with the backtick method but change the syntax a bit and keep the : 1 part like so:
$inc: { [`uniqueVisitors.country.${req.body.country}`]: 1 }

mongodb $pull doesn't work with array in subdocument

I have a problem with an update with MongoDB.
My schema look like this:
Project: {
_id: ObjectId(pro_id)
// some data
dashboard_group: [
{
_id: ObjectId(dgr_id)
dgr_name: "My Dashboard"
dgr_tasks: [
id1,
id2,
...
]
},
// other dashboards
]
}
I want to remove id2 but the $pull operator seems not work. Mongo return me this :
result: {
lastErrorObject: {
n: 1,
updatedExisting: true
},
ok: 1
}
This is my request:
db.Project.findOneAndUpdate({
"dashboard_group._id": dgr_id
}, {
$pull: {
"dashboard_group.$.dgr_tasks": id2
}
});
dgr_id is already cast to ObjectId before the query and I verified the value that I want to remove.
Can anyone have an idea ?
You will need to select the particular array element using "$elemMatch" like this
Query : {"dashboard_group":{"$elemMatch":{dgr_name:"My Dashboard"}}}
Update : {$pull:{"dashboard_group.$.dgr_tasks":"id2"}}
So, I found a solution with the $[] identifier. It's not its basic utility, but it fit to my case.
A task ID cannot be at 2 location, it belongs to 1 and only 1 dashboard. So if you make a request like :
db.Project.findOneAndModify({
"dashboard_group._id": dgr_id
}, {
$pull: {
"dashboard_group.$[].dgr_tasks": id2
}
});
Mongo will remove all value that match id2. Without the {multi: true} option, it will make the update 1 time, and my item is indeed remove from my nested array.

I want to update sub document in array

I now use mongooses to pull and pull subdocuments to the array, and now I want to change the contents of the detail field of that subdocument with the _id of the subdocument.
{
subDocument: [{
_id: ObjectId('123'),
detail: 'I want update this part'
}]
}
I tried to use the $set method as shown below but it did not work as expected.
Model.findByIdAndUpdate(uid, { $Set: {subDocument: {_id: _id}}});
Looking at the for statement as shown below is likely to have a bad effect on performance. So I want to avoid this method.
const data = findById(uid);
for(...) {
if(data.subDocument[i]._id==_id) {
data.subDocument[i].detail = detail
}
}
Can you tell me some mongodb queries that I can implement?
And, Is it not better to use the 'for(;;)' statement shown above than to search using mongodb's query?
This should work:
Model.findOneAndUpdate({"subdocument._id": uid},
{
$set: {
"subdocument.$.detail ": "detail here"
}
},
).exec(function(err, doc) {
//code
});
To find subdocument by id, I am using something like this :
var subDocument = data.subDocument.id(subDocumentId);
if (subDocument) {
// Do some stuff
}
else {
// No subDocument found
}
Hope it helps.

How to update a field using its previous value in MongoDB/Mongoose

For example, I have some documents that look like this:
{
id: 1
name: "foo"
}
And I want to append another string to the current name field value.
I tried the following using Mongoose, but it didn't work:
Model.findOneAndUpdate({ id: 1 }, { $set: { name: +"bar" } }, ...);
Edit:
From Compatibility Changes in MongoDB 3.6:
MongoDB 3.6.1 deprecates the snapshot query option.
For MMAPv1, use hint() on the { _id: 1} index instead to prevent a cursor from returning a document more than once if an intervening write operation results in a move of the document.
For other storage engines, use hint() with { $natural : 1 } instead.
Original 2017 answer:
You can't refer to the values of the document you want to update, so you will need one query to retrieve the document and another one to update it. It looks like there's a feature request for that in OPEN state since 2016.
If you have a collection with documents that look like:
{ "_id" : ObjectId("590a4aa8ff1809c94801ecd0"), "name" : "bar" }
Using the MongoDB shell, you can do something like this:
db.test.find({ name: "bar" }).snapshot().forEach((doc) => {
doc.name = "foo-" + doc.name;
db.test.save(doc);
});
The document will be updated as expected:
{ "_id" : ObjectId("590a4aa8ff1809c94801ecd0"), "name": "foo-bar" }
Note the .snapshot() call.
This ensures that the query will not return a document multiple times because an intervening write operation moves it due to the growth in document size.
Applying this to your Mongoose example, as explained in this official example:
Cat.findById(1, (err, cat) => {
if (err) return handleError(err);
cat.name = cat.name + "bar";
cat.save((err, updatedCat) => {
if (err) return handleError(err);
...
});
});
It's worth mentioning that there's a $concat operator in the aggregation framework, but unfortunately you can't use that in an update query.
Anyway, depending on what you need to do, you can use that together with the $out operator to save the results of the aggregation to a new collection.
With that same example, you will do:
db.test.aggregate([{
$match: { name: "bar" }
}, {
$project: { name: { $concat: ["foo", "-", "$name"] }}
}, {
$out: "prefixedTest"
}]);
And a new collection prefixedTest will be created with documents that look like:
{ "_id" : ObjectId("XXX"), "name": "foo-bar" }
Just as a reference, there's another interesting question about this same topic with a few answers worth reading: Update MongoDB field using value of another field
If this is still relevant, I have a solution for MongoDB 4.2.
I had the same problem where "projectDeadline" fields of my "project" documents were Array type (["2020","12","1"])
Using Robo3T, I connected to my MongoDB Atlas DB using SRV link. Then executed the following code and it worked for me.
Initial document:
{
_id : 'kjnolqnw.KANSasdasd',
someKey : 'someValue',
projectDeadline : ['2020','12','1']
}
CLI Command:
db
.getCollection('mainData')
.find({projectDeadline: {$not: {$eq: "noDeadline"}}})
.forEach((doc) => {
var deadline = doc.projectDeadline;
var deadlineDate = new Date(deadline);
db
.mainData
.updateOne({
_id: doc._id},
{"$set":
{"projectDeadline": deadlineDate}
}
)}
);
Resulting document:
{
_id : 'kjnolqnw.KANSasdasd',
someKey : 'someValue',
projectDeadline : '2020-12-01 21:00:00.000Z'
}

Nodejs Mongo insert into subdocument - dynamic fieldname

{username:'me', companies:{"yourcompany":{...}}
I want to insert a company into a user record (user collection), to make:
{username:'me', companies:{ "yourcompany":{...}, "mycompany":{...} }
But the name is dynamic..
var companyid = "mycompany";
.collection('users').findAndModify(
{username: usern},
[['_id', 'asc']],
{$set:{companies:{companyid: { desksmemberships:[] }}}},
{new: true}, function(){...}
Gives this.. {username:'me', companies:{ "yourcompany":{...}, "companyid":{...} }
How do I do this?
You'd have to build up your $set modifier programmatically:
var modifier = { $set: {} };
modifier.$set['companies.' + companyid] = { desksmemberships:[] };
And then use modifier as the third parameter in your findAndModify call.
You may also want to consider changing companies to be an array instead of an embedded object.
Node.js 4.x Update
You can now use the computed property syntax to do this directly in the object literal:
collection('users').findAndModify(
{username: usern},
[['_id', 'asc']],
{$set:{['companies.' + companyid]: { desksmemberships:[] }}},
{new: true},
function(){...});
I've seen this question on quite a few posts, some of them quite complex- call me crazy but you can do this (node + mongo):
My db schema 'matches' is set to an array. In the mongo data [match.matchId] becomes '2028856183'.
db.update({key: value}, {matches: [{[match.matchId] : match}] }, callback);
db.update(what to find, what to change it to, what to do next)
You can use any variable in the brackets.

Resources