How to update in Mongoose query Object - node.js

I need to change the status to Y for particular comment if i click approved button in UI, My document structure is below.
{
"_id" : ObjectId("5a1fd0ffef39ff11ae353d10"),
"file_name" : "Profile",
"file_type" : "docx",
"created_date" : ISODate("2017-11-28T10:29:10.373Z"),
"updated_date" : ISODate("2017-11-28T12:39:32.148Z"),
"comments" : [
{
"created_date" : ISODate("2017-11-28T13:23:51.472Z"),
"status" : "N",
"comment_text" : "Yes...",
"username" : "Vishnu"
},
{
"created_date" : ISODate("2017-11-28T13:24:15.938Z"),
"status" : "N",
"comment_text" : "Yes...",
"username" : "Vishnu"
},
{
"created_date" : ISODate("2017-11-28T13:28:44.455Z"),
"status" : "N",
"comment_text" : "fsdfdsf",
"username" : "T"
},
{
"created_date" : ISODate("2017-11-28T13:29:22.132Z"),
"status" : "N",
"comment_text" : "fdsfsdf",
"username" : "dasdas"
},
{
"created_date" : ISODate("2017-11-28T13:29:46.247Z"),
"status" : "N",
"comment_text" : "fdgdfgfd",
"username" : "Vishnu T"
}
]
}
I have tried some query which was already in stackover flow like
mongo.filemanager.update(
{ "_id": req.body.id, "comments.comment_text": req.body.comments },
{ "$set": { "comments.$.status": 'Y' } }
)
But status value is not get changing. am using mongoose here.
Kindly help me in this issue.. Thanks In advance
Updated
mongo.filemanager.findOneAndUpdate(
{ "_id": req.body.id, "comments.comment_text": req.body.comments },
{
"$set": { "comments.$.status": 'Y' }
},
function(err,doc) {
if (err) throw err
console.log("Updated Commentstatus")
}
);

Here is the syntax of update in mongoose.
Model.update(conditions, update, options, callback);
So,
$set should be the second paramter,
try
mongo.filemanager.update({ "_id": req.body.id, "comments.comment_text": req.body.comments },{ "$set": { "comments.status": 'Y' } })
Mongoose official Documentation
You can also use findOneAndUpdate,
mongo.filemanager.findOneAndUpdate(
{ "_id": req.body.id, "comments.comment_text": req.body.comments },
{
"$set": { "comments.status": 'Y' }
},
function(err,doc) {
}
);

I've made an example that look like your case, interesting.
You can simulate mongodb terminal online in here for free

Related

How to update multiple records with multiple values in mongo db and node js?

I have few records in mongo db and i need to update all those records with different values at a single stretch which matches its "id".
The record in mongo db will not have value for "status" & "timestamp" and i need to update these.
When i try to update the record, the update values are getting newly inserted into another new collection and not updating in my existing collection.
var parsedRes = [{
"id": "1000000",
"mobilenum": "2323442343",
"code": "SC0",
"status": "1",
"timestamp": "1602771846"
}, {
"id": "1000001",
"mobilenum": "3323442343",
"code": "SC0",
"status": "8",
"timestamp": "1602771843"
}, {
"id": "1000002",
"mobilenum": "4323442343",
"code": "SC0",
"status": "3",
"timestamp": "1602771842"
}, {
"id": "1000003",
"mobilenum": "5323442343",
"code": "SC0",
"status": "1",
"timestamp": "1602771844"
}
]
var updateData = [];
for (var i = 0; i < parsedRes.length; i++) {
updateData.push({
updateOne: {
filter: { ID: parsedRes[i].id },
update: {
$set: {
mobile: parsedRes[i].mobilenum,
code: parsedRes[i].code,
status: parsedRes[i].status,
timestamp: parsedRes[i].timestamp
}
}, upsert: true
}
});
}
return new Promise(function (resolve, reject) {
mongomodel.bulkWrite(updateData, { ordered: false }, async function(err, result) {
if (err) {
console.log("errorrrrrrrr : ",err)
reject(err)
} else {
console.log("result : ****************** ",result)
resolve(result)
}
})
updateData = [];
})
Could anyone please help me on where i go wrong. Is there any other approach to do this, please share. Thanks in advance.
sample document from db:
#wak786 Please find the sample document
db.mongomodel.find()
{ "_id" : ObjectId("5f896c9d1148a810d90a8261"), "id" : "1000000", "status" : "1", "mobilenum" : "2323442343", "code" : "TA0","UpdateDate" : "1602771841", "createdAt" : ISODate("2020-10-16T09:49:17.693Z"), "updatedAt" : ISODate("2020-10-16T09:49:17.693Z") }
{ "_id" : ObjectId("5f896c9d1148a810d90a8262"), "id" : "1000001", "status" : "8", "mobilenum" : "3323442343", "code" : "OR0","UpdateDate" : "1602771841", "createdAt" : ISODate("2020-10-16T09:49:17.693Z"), "updatedAt" : ISODate("2020-10-16T09:49:17.693Z") }
{ "_id" : ObjectId("5f896c9d1148a810d90a8263"), "id" : "1000002", "status" : "1", "mobilenum" : "4323442343", "code" : "OC0","UpdateDate" : "1602771841", "createdAt" : ISODate("2020-10-16T09:49:17.693Z"), "updatedAt" : ISODate("2020-10-16T09:49:17.693Z") }
{ "_id" : ObjectId("5f896c9d1148a810d90a8264"), "id" : "1000003", "status" : "1", "mobilenum" : "5323442343", "code" : "TC0","UpdateDate" : "1602771841", "createdAt" : ISODate("2020-10-16T09:49:17.693Z"), "updatedAt" : ISODate("2020-10-16T09:49:17.693Z") }
Some of the things I notice here are :
You are using upsert:true. It will add new documents to collection if your filter query doesn't find any matching documents. SO setting upsert:false will solve the problem of new documents getting added to collection.
Another problem I suspect is in following line of code.
filter: { ID: parsedRes[i].id },
I think it should be id instead of ID (I am assuming it is not a Javascript constant).
If you have const ID = "id" somewhere in your code then ignore this point.
EDIT :-
Can you try by removing $set becoz what I see in latest mongoose docs, $set is not needed anymore.
This is from mongoose docs.
https://mongoosejs.com/docs/api.html#model_Model.bulkWrite
{
updateOne: {
filter: { name: 'Eddard Stark' },
// If you were using the MongoDB driver directly, you'd need to do
// `update: { $set: { title: ... } }` but mongoose adds $set for
// you.
update: { title: 'Hand of the King' }
}
},

update particular single subdocument in nested array in nodejs mongodb

I am new to nodejs and mongo db. I have nested subdocument type schema in mongo. Document type is Process => Subprocess => tasks => configs. These are in the format of nested arrays and can be multiple.
{
"_id" : ObjectId("5bcec0ee711fe511f4848c1d"),
"process_title" : "customer acquisition",
"subprocess" : [
{
"_id" : ObjectId("5bcec0f8711fe511f4848c1e"),
"subprocess_title" : "application",
"tasks" : [
{
"_id" : ObjectId("5bcec158711fe511f4848c1f"),
"task_title" : "pre screening",
"task_slug" : "pre-screening",
"task_configs" : [
{
"_id" : ObjectId("5bcec4b912582b01b84fe47a"),
"next_task" : "thanks"
}
]
},
{
"_id" : ObjectId("5bcec190711fe511f4848c20"),
"task_title" : "thanks",
"task_slug" : "thanks",
"task_configs" : [
{
"_id" : ObjectId("5bcec469ab23ab1fc0bbb9ed"),
"form_field" : "responseMessage",
"expression" : "=",
"expression_value" : "Approved4",
"success_task" : "signup",
"success_sub_process_id" : "5bcec0f8711fe511f4848c1e",
"fail_task" : "thanks",
"fail_sub_process_id" : "5bcec0f8711fe511f4848c1e"
}
]
},
{
"_id" : ObjectId("5bcec1c3711fe511f4848c21"),
"task_title" : "signup",
"task_slug" : "signup",
"task_configs" : [
{
"_id" : ObjectId("5bcec469ab23ab1fc0bbb9ed"),
"form_field" : "responseMessage",
"expression" : "=",
"expression_value" : "Approved4",
"success_task" : "signup",
"success_sub_process_id" : "5bcec0f8711fe511f4848c1e",
"fail_task" : "thanks",
"fail_sub_process_id" : "5bcec0f8711fe511f4848c1e"
}
]
}
]
}
],
"created_at" : ISODate("2018-10-23T06:34:22.676Z"),
"updated" : ISODate("2018-10-23T06:34:22.676Z"),
"__v" : 0
}
Now I want to update task_configs for a particular task. I am updating task_config on the basis of task_slug.
I found a way myself to update above query.
Workflow.updateOne({ "_id": new ObjectId(req.body.process_id), "subprocess._id": new ObjectId(req.body.sub_process_id), "subprocess.tasks.task_slug": req.body.task_slug },
{
$set: {
'subprocess.$.tasks.$[j].task_configs': req.body.task_configs
}
},
{
arrayFilters: [
{
"j.task_configs": req.body.task_configs
}]
})
.then(function (resp) {
console.log(resp)
res.json({ status: 'success', resp });
}).catch(function (err) {
res.status(500).json(err);
})

How to count the number of values found for a field in MongoDB?

I have to find "exitState" : this is single document , if multiple documents how to find.
{
"_id" : "abc",
"exitType" : "Hang",
"exitState" : "INDIA",
"outcome" : "Successful",
"CEV" : [
{
"LogID" : "CEV",
"ReportingMode" : "N",
"Log_DateTime" : "02:23:2016 00:17:48:913",
"Log_TS" : NumberLong(1456186668913),
"ServiceType" : "TEL",
"MsgID" : "25000",
"SysName" : "test123",
"ProcessID" : "9611",
"Port" : "0",
"ModuleName" : "ArcCDR::CDR_CustomEvent",
"AppName" : "testVXML2",
"MsgTxt" : "abc::24::Test::outcome=Successful$$$exitType=Hang$$$exitState=INDIA",
"Record_Key" : "abc",
"Token1" : "24",
"CustomerName" : "Test",
"CEV_MsgTxt" : "outcome=Successful$$$exitType=Hang$$$exitState=INDIA",
"outcome" : "Successful",
"exitType" : "Hang",
"exitState" : "INDIA"
}
],
"language" : "ENGLISH",
"SC_TS" : ISODate("2016-02-23T00:17:06.060+0000"),
"SC_TimeMS" : NumberLong(1456186626060),
"CDR_SC" : {
"LogID" : "CDR",
"ReportingMode" : "N",
"Log_DateTime" : "02:23:2016 00:17:06:060",
"Log_TS" : NumberLong(1456186626060),
"ServiceType" : "TEL",
"MsgID" : "20010",
"SysName" : "test123",
"ProcessID" : "9611",
"Port" : "0",
"ModuleName" : "TEL_AnswerCall",
"AppName" : "testVXML2",
"MsgTxt" : "abc:SC:testVXML2:452:607856:0223201600170606::",
"Record_Key" : "abc",
"CDR_Type" : "SC",
"Token2" : "testVXML2",
"Token3" : "452",
"Token4" : "607856",
"Token5" : "0223201600170606"
},
" SC_TS_TZ" : ISODate("2016-02-23T00:17:06.060+0000"),
"EC_TS" : ISODate("2016-02-23T00:17:48.910+0000"),
"EC_TS_TZ" : ISODate("2016-02-23T00:17:48.910+0000"),
"EC_TimeMS" : NumberLong(1456186668910),
"CDR_EC" : {
"LogID" : "CDR",
"ReportingMode" : "N",
"Log_DateTime" : "02:23:2016 00:17:48:910",
"Log_TS" : NumberLong(1456186668910),
"ServiceType" : "TEL",
"MsgID" : "20011",
"SysName" : "test123",
"ProcessID" : "9611",
"Port" : "0",
"ModuleName" : "TEL_SRRecognizeV2",
"AppName" : "testVXML2",
"MsgTxt" : "abc:EC:02:0223201600174891::",
"Record_Key" : "abc",
"CDR_Type" : "EC",
"Token2" : "02",
"Token3" : "0223201600174891"
},
"CustomerName" : "Test"
}
Below is my query but unable to find exitState in all documents . Can you please?
dbo.ProductModel.aggregate([
{$match: {"EC_TS":{$gte:new Date(start.toISOString()), $lte:new Date(end.toISOString())}} },
{$group:
{_id: '$exitState', count : {$sum: 1} }
}
]).toArray(function(err, result4) {
console.log(+ result4[0]["exitState"]);
console.log("Total exitState=" + result4[0]["total"]);
q4result=(result4[0]["total"]);
});
});
Maybe you can filter the results:
const result5 = result4.filter((result) => result.exitState && result.exitState !== '');
const nbResults = result5.length;
db.tablename.find({},{"exitStates":1}).count()
https://www.w3resource.com/mongodb-exercises/mongodb-exercise-4.php
I can't understand what is your question exactly. if you want to know how many docs exist in the collection and count them by their exitState, this function retuns what you want. I don't know $match works like this or not, But please log the result for test before doing any action on it.
dbo.ProductModel.aggregate([
{ $match: { "EC_TS": { $gte: new Date( start.toISOString() ),
$lte: new Date( end.toISOString() ) } } },
{ $group: {_id: '$exitState', count : {$sum: 1} } }
], (err, result) => {
if (err) throw err;
console.log(result);
// result is like this:
// [ {"_id": "INDIA", "count": 3}, {"_id": "US", "count": 8} ]
});

Mongo pull object from array inside array

i have inside my mongoDB collection this document
{
"_id" : ObjectId("5b633025579fac22e74bf3be"),
"FLAGS" : [
{
"toSent" : [
{
"_id" : ObjectId("5b633025579fac22e74bf3c2"),
"phone" : "+84404040404"
},
{
"_id" : ObjectId("5b633025579fac22e74bf3c1"),
"phone" : "+212652253403"
},
{
"_id" : ObjectId("5b633025579fac22e74bf3c0"),
"phone" : "+212123456788"
}
],
"_id" : ObjectId("5b633025579fac22e74bf3bf"),
"action" : "group_p_a"
},
{
"toSent" : [
{
"_id" : ObjectId("5b633031579fac22e74bf3c9"),
"phone" : "+212651077199"
},
{
"_id" : ObjectId("5b633031579fac22e74bf3c8"),
"phone" : "+84404040404"
},
{
"_id" : ObjectId("5b633031579fac22e74bf3c7"),
"phone" : "+212652253403"
},
{
"_id" : ObjectId("5b633031579fac22e74bf3c6"),
"phone" : "+212123456788"
}
],
"_id" : ObjectId("5b633031579fac22e74bf3c5"),
"action" : "group_p_a"
}
],
"time" : ISODate("2018-08-02T16:24:05.747+0000"),
"action_user_phone" : "+212123456788",
"idGroup" : "e534379a-1580-4568-b5ec-6eaf981538d2",
"nomGroup" : "MOH FOR EVER",
"__v" : NumberInt(0)
}
TODO
I need to remove for example this element { "_id" : ObjectId("5b633025579fac22e74bf3c2"), "phone" : "+84404040404"}
WHAT I DID
GroupEvents.update({}, {$pull:{FLAGS:{$elemMatch:{toSent:{phone: "+84404040404"} }}}},function(err,ret){
if(err)
console.log("error"+err);
if(ret)
console.log(ret);
});
It remove all what's inside toSent event if it doesn't match.
Any help please
You need to use $ positional operator instead of $elemMatch here
GroupEvents.update(
{ "Flags.toSent.phone": "+84404040404" },
{ "$pull": { "FLAGS.$.toSent": { "phone": "+84404040404" }}},
)
If you want remove from every element of FLAGS array this you need to use $[] the all positional operator
GroupEvents.update(
{ "Flags.toSent.phone": "+84404040404" },
{ "$pull": { "FLAGS.$[].toSent": { "phone": "+84404040404" }}},
)

How to use '$match' Aggregation Operators of MongoDB to match with the id of embedded document?

Scenario: Consider the document present in the MongoDB in collection named 'MyCollection'
{
"_id" : ObjectId("512bc95fe835e68f199c8686"),
"author": "dave",
"score" : 80,
"USER" : {
"UserID": "Test1",
"UserName": "ABCD"
}
},
{ "_id" : ObjectId("512bc962e835e68f199c8687"),
"author" : "dave",
"score" : 85,
"USER" : {
"UserID": "Test2",
"UserName": "XYZ"
}
},
...
I know the UserID and want to fetch based on that.
Issue: I tried the following code with Node.js + MongoDB-native driver:
db.Collection('MyCollection', function (err, collection) {
if (err) return console.error(err);
collection.aggregate([
{ $match: { '$USER.UserID': 'Test2'} },
{$group: {
_id: '$_id'
}
},
{
$project: {
_id: 1
}
}
], function (err, doc) {
if (err) return console.error(err);
console.dir(doc);
});
});
But its not working as expected.
Question: Can anyone know how to do the same with $match operator in MongoDB query?
Update: I am not getting any error. But the object will be blank i.e. []
I tried in the shell and your $match statement is wrong - trying in the shell
> db.MyCollection.find()
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "USER" : { "UserID" : "Test1", "UserName" : "ABCD" } }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "USER" : { "UserID" : "Test2", "UserName" : "XYZ" } }
> db.MyCollection.aggregate([{$match: {"$USER.UserID": "Test2"}}])
{ "result" : [ ], "ok" : 1 }
> db.MyCollection.aggregate([{$match: {"USER.UserID": "Test2"}}])
{
"result" : [
{
"_id" : ObjectId("512bc962e835e68f199c8687"),
"author" : "dave",
"score" : 85,
"USER" : {
"UserID" : "Test2",
"UserName" : "XYZ"
}
}
],
"ok" : 1
}
So the full aggregation would be:
db.MyCollection.aggregate([
{$match: {"USER.UserID": "Test2"}},
{$group: {"_id": "$_id"}},
{$project: {"_id": 1}}
])
(You don't need the extra $project as you only project _id in the $group but equally as _id is unique you should just have the $project and remove the $group)

Resources