MongoDB update collection after a while - mongodb

I have the following collection:
{ "_id" : ObjectId("5b0422b086698f39b436aa42"), "_uid" : "bQVAKHg7WWgnANHFiZOleo2FUsi4", "role" : "USER", "documentType" : "CC", "documentNumber" : 123148544, "displayName" : "Carlos Perry", "email" : "CPerry#todo.com", "phoneNumber" : "+573003004545", "photoURL" : "url", "idNetwork" : "idNetwork", "blocked" : false, "blockReason" : "Malware detection", "isDisabled": false, "lastLogin" : 1526313028249 }
{ "_id" : ObjectId("5b0423494bd76b3a308c5115"), "_uid" : "bQVAKHg7WWgnANHFiZOleo2FUsi3", "role" : "USER", "documentType" : "CC", "documentNumber" : 534567878, "displayName" : "Julian Cruz", "email" : "Jcruz#algo.com", "phoneNumber" : "+573003004545", "photoURL" : "url", "idNetwork" : "idNetwork", "blocked" : false, "blockReason" : "Malware detection", "isDisabled" :false, "lastLogin" : 1526313028249 }
And i want to update the field "lastLogin" five minutes after the user is logged into the application, but don't found a way to update mongo after five minutes.
I'm using Mongoose 5.0.3 on Node JS 9.4.0 and Mongo 3.6.2

Related

Mongoose Return One Object from Array

I use mongoose and I have this data in my mongodb :
"_id" : ObjectId("5f13e1edf7c56a896987c191"),
"deleted" : false,
"email" : "klinikkoding#gmail.com",
"firstName" : "Klinik",
"lastName" : "Koding",
"resetToken" : null,
"workspaces" : [
{
"status" : "active",
"_id" : ObjectId("5f13e124f7c56a896987c18e"),
"code" : "kk",
"name" : "Klinik Koding",
"_roleId" : ObjectId("5f13de3eb33fa33ce2a3b0dd")
},
{
"status" : "invited",
"_id" : ObjectId("5f13e13ff7c56a896987c190"),
"code" : "rm",
"name" : "The Manage",
"_roleId" : ObjectId("5f13de3eb33fa33ce2a3b0dd")
}
],
How can I return only one workspace? The output that I need is like this:
"_id" : ObjectId("5f13e1edf7c56a896987c191"),
"deleted" : false,
"email" : "klinikkoding#gmail.com",
"firstName" : "Klinik",
"lastName" : "Koding",
"resetToken" : null,
"workspaces" : [
{
"status" : "active",
"_id" : ObjectId("5f13e124f7c56a896987c18e"),
"code" : "kk",
"name" : "Klinik Koding",
"_roleId" : ObjectId("5f13de3eb33fa33ce2a3b0dd")
},
],
Anyone can help me with this query?
Try this.
async function retrieve(){
// You can retrieve it by any field. I used email because it is unique.
let data=await yourModelName.findOne({email:"klinikkoding#gmail.com"})
data.workspaces.splice(1,1)
console.log("your final result",data)
}
retrieve()

Nested object update query in mongodb

I have object in my procedures collection.
I want to update item_status of item_id 5996c80fca423ce1228f7690 which is available in preferences.items.
I want to get all records who has mentioned item_id so that we can update all occurrences.
{
"_id" : ObjectId("5996d0a1ca423ce1228f777a"),
"status" : "Active",
"procedure_name" : "ATHA",
"procedure_for" : "Admin",
"created_by" : "5940c3e8734d1d79866338cf",
"created_on" : ISODate("2017-10-19T18:44:22.702+0000"),
"speciality_id" : "5751131e3a1253845560a984",
"speciality" : "Orthopdics",
"master_template_id" : "",
"hospital_id" : "",
"surgeon_nurse_id" : "",
"procedure_type" : "Published",
"published_on" : ISODate("2017-10-19T18:44:22.702+0000"),
"surgical_sequence" : [
],
"preferences" : [
{
"category_id" : "5971fae84658f5241d8a5b70",
"category" : "Instruments",
"_id" : ObjectId("59e8f2861c999f292a837304"),
"items" : [
{
"item_id" : "5996c80fca423ce1228f7690",
"item_name" : "Battery",
"item_description" : "",
"image_name" : "BATTERY.png",
"icon_image" : "",
"side_view_image" : "",
"_id" : ObjectId("59e8f2861c999f292a837306"),
"attributes" : [
],
"subcategory" : [
{
"name" : "Power tool",
"subcategory_id" : "5996c80eca423ce1228f7549",
"parent_id" : "",
"_id" : ObjectId("5996c80fca423ce1228f7709")
},
{
"name" : "Battery",
"subcategory_id" : "5996c80eca423ce1228f750e",
"parent_id" : "5996c80eca423ce1228f7549",
"_id" : ObjectId("5996c80fca423ce1228f7708")
}
]
}
]
}
],
"__v" : NumberInt(0),
"modified_by" : "5940c3e8734d1d79866338cf",
"modified_on" : ISODate("2017-10-19T18:44:22.702+0000")
}
I'm assuming you are talking about updating status field since I don't find any field with the name item_status. If that's the case, the below query should work:
> db.procedures.updateMany({"preferences.$.items.$.item_id": "5996c80fca423ce1228f7690"}, {"$set": {"status": "new_status"}})

How to get a list of nested object only if condition match in mongodb?

I am developing application which had company and there are 2 users reviewer & admin inside that company. Now as a super admin I want to display only Reviewer of the all company. with following information
Company Name,
Reviewer Name,
Reviewer Email
My collection is
{
"_id" : ObjectId("58830988b9c5f808fc307ec4"),
"createdat" : ISODate("2017-01-21T07:11:04.218+0000"),
"isActive" : true,
"users" : [
{
"_id" : ObjectId("58830988b9c5f808fc307ec5"),
"createdat" : ISODate("2017-01-21T07:11:04.219+0000"),
"role" : "admin",
"password" : "test",
"email" : "email3#email.com",
"name" : "test"
},
{
"name" : "test",
"email" : "email2#email.com",
"password" : "test",
"role" : "admin",
"createdat" : ISODate("2017-01-21T07:24:42.559+0000"),
"_id" : ObjectId("58830cba24ebfa04f812e544")
},
{
"name" : "test",
"email" : "email1#email.com",
"password" : "test",
"role" : "admin",
"isActive" : true,
"createdat" : ISODate("2017-01-21T07:36:37.337+0000"),
"_id" : ObjectId("58830f85d92f1808e0984306")
},
{
"name" : "reviewName 1",
"email" : "reviewName1#gmail.com",
"password" : "test",
"role" : "reviewer",
"isActive" : true,
"createdat" : ISODate("2017-01-21T07:55:39.727+0000"),
"_id" : ObjectId("588313fba001380a383e077e")
},
{
"name" : "test",
"email" : "test",
"password" : "test",
"role" : "admin",
"isActive" : true,
"createdat" : ISODate("2017-01-21T07:56:52.963+0000"),
"_id" : ObjectId("588314447bbc230c306804c1")
}
],
"companyname" : "Test Company 1",
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("58830988b9c5f808fc307ec4"),
"createdat" : ISODate("2017-01-21T07:11:04.218+0000"),
"isActive" : true,
"users" : [
{
"_id" : ObjectId("58830988b9c5f808fc307ec5"),
"createdat" : ISODate("2017-01-21T07:11:04.219+0000"),
"role" : "admin",
"password" : "test",
"email" : "test4#email.com",
"name" : "test"
},
{
"name" : "test",
"email" : "test3#email.com",
"password" : "test",
"role" : "admin",
"createdat" : ISODate("2017-01-21T07:24:42.559+0000"),
"_id" : ObjectId("58830cba24ebfa04f812e544")
},
{
"name" : "test",
"email" : "test2#email.com",
"password" : "test",
"role" : "admin",
"isActive" : true,
"createdat" : ISODate("2017-01-21T07:36:37.337+0000"),
"_id" : ObjectId("58830f85d92f1808e0984306")
},
{
"name" : "reviewName 2_1",
"email" : "reviewName2_1#gmail.com",
"password" : "test",
"role" : "reviewer",
"isActive" : true,
"createdat" : ISODate("2017-01-21T07:55:39.727+0000"),
"_id" : ObjectId("588313fba001380a383e077e")
},
{
"name" : "reviewName 2_2",
"email" : "reviewName2_2#gmail.com",
"password" : "test",
"role" : "reviewer",
"isActive" : true,
"createdat" : ISODate("2017-01-21T07:56:52.963+0000"),
"_id" : ObjectId("588314447bbc230c306804c1")
}
],
"companyname" : "Test Company 2",
"__v" : NumberInt(0)
}
So I would Like to get Following Data
Company Name : Test Company 1,
Reviewer Name : reviewName 1,
Reviewr Email : reviewName1#gmail.com
Company Name : Test Company 2,
Reviewer Name : reviewName 2_1,
Reviewer Email : reviewName2_1#gmail.com
Company Name : Test Company 2,
Reviewer Name : reviewName 2_2,
Reviewer Email : reviewName2_2#gmail.com
There will be lot's of companies, so I don't want to go through with loop and use require information in array. because there will be thousands of company and each company have hundreds of reviewer and admin. So If we go through loop then it will be time consuming. I want to get this data only from collection. So I am looking for the query or condition which gives me only require information.
Can someone help me to get these data-only.
You need to use MongoDB aggregation framework for getting the data according to your requirement.
This query will return data in the desired form.
db.collection.aggregate([
{
"$unwind": "$users"
},
{
"$match": {
"users.role":"reviewer"
}
},
{
"$project": {
"_id":0,
"Company Name" : "$companyname",
"Reviewer Name" : "$users.name",
"Reviewer Email" : "$users.email"
}
}
])

Inconsistent query results with embedded documents on MongoDB

I've got a collection called payments with an example of its document shown below:
{
"_id" : ObjectId("579b5ee817e3aaac2f0aebc1"),
"updatedAt" : ISODate("2016-07-29T11:04:01.209-03:00"),
"createdAt" : ISODate("2016-07-29T10:49:28.113-03:00"),
"createdBy" : ObjectId("5763f56010cd7b03008147d4"),
"contract" : ObjectId("578cb907f1575f0300d84d09"),
"recurrence" : [
{
"when" : ISODate("2016-05-29T11:03:45.606-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2d"),
"transaction" : {
"createdAt" : ISODate("2016-05-29T11:03:45.608-03:00"),
"tid" : "9999999999999999B01A",
"status" : 4,
"code" : "00",
"message" : "Transação autorizada"
},
"status" : "PAGO"
},
{
"when" : ISODate("2016-06-29T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2c"),
"transaction" : {
"createdAt" : ISODate("2016-06-29T11:03:45.608-03:00"),
"tid" : "9999999999999999B01A",
"status" : 4,
"code" : "00",
"message" : "Transação autorizada"
},
"status" : "PAGO"
},
{
"when" : ISODate("2016-07-29T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2b"),
"status" : "ERRO",
"transaction" : {
"code" : "56",
"createdAt" : ISODate("2016-07-29T11:04:01.196-03:00"),
"message" : "Autorização negada",
"status" : 5,
"tid" : "1006993069000730B88A"
}
},
{
"when" : ISODate("2016-07-30T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2a"),
"status" : "PENDENTE"
},
{
"when" : ISODate("2016-07-31T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e29"),
"status" : "PENDENTE"
},
{
"when" : ISODate("2016-08-01T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e28"),
"status" : "PENDENTE"
}
],
"status" : "PAGO",
"conditions" : {
"originalValue" : 7406.64,
"totalValue" : 7400,
"upfrontValue" : 1500,
"upfrontInstallments" : 3,
"balanceInstallments" : 9
},
"__v" : 0,
"transaction" : {
"code" : "00",
"createdAt" : ISODate("2016-07-29T10:49:46.610-03:00"),
"message" : "Transação autorizada",
"status" : 6,
"tid" : "1006993069000730AF5A"
}
}
If I run the query below, I get the desired document shown above:
db.payments.find({ "recurrence.transaction.tid": "1006993069000730B88A" })
However, if I run this other query, MongoDB returns my entire collection (presumably because it didn't match the subdocument's id):
db.payments.find({ "recurrence._id": ObjectId("579b6241ea945e3631f64e2b") })
Both queries should return the same result! I also checked some other questions including this one so unless I'm going crazy I'm doing the same thing. Not sure why the inconsistent results though.
Tryout this:
db.payments.find({ recurrence : { $elemMatch: { "transaction.tid": "1006993069000730B88A"} } }).pretty()

Query to filter the information from a mongodb collection

I have to retrieve a list of users from list of documents which matches the condition. The document structure look like below
{
"_id" : ObjectId("660ff865d4f9075d40a1101c"),
"orderFormId" : "OF-rJw4elBYK",
"orderDetails" : [
{
"courseId" : "53fc31f443fa1fe885d3ad61",
"userInfo" : [
{
"dob" : "2015-03-22T18:30:00.000Z",
"lastName" : "M",
"status" : "Pending Appproval",
"eMail" : "jihin345#baabte.com",
"firstName" : "Arun"
},
{
"status" : "requested",
"firstName" : "asdasd",
"dob" : "2015-03-23T18:30:00.000Z",
"lastName" : "asdafasd",
"userId" : "RQ-11xDPALgR",
"eMail" : "adsasd#baabte.com"
},
{
"status" : "requested",
"firstName" : "asdaf",
"dob" : "2015-03-23T18:30:00.000Z",
"lastName" : "fsdsdf",
"userId" : "RQ-OdoXAOLrB",
"eMail" : "ashdjasufh#baabte.com"
},
{
"status" : "requested",
"firstName" : "asdas",
"dob" : "2015-03-23T18:30:00.000Z",
"lastName" : "asdasd",
"userId" : "RQ-Bw2Xokmda",
"eMail" : "asdasd#gmail.com"
}
],
"userCount" : 5,
"Name" : "Compilers",
"coursePrice" : 1000,
"coursetype" : "offline"
},
{
"courseId" : "53fc31f443fa1fe885d3ad62",
"userInfo" : [
{
"dob" : "2015-03-22T18:30:00.000Z",
"lastName" : "Raj",
"status" : "requested",
"eMail" : "jihin432#baabte.com",
"firstName" : "Nithul"
},
{
"dob" : "2015-03-22T18:30:00.000Z",
"lastName" : "P C",
"status" : "requested",
"eMail" : "jihin345#baabte.com",
"firstName" : "Kahyoom"
}
],
"userCount" : 1,
"Name" : "Computer Science 101",
"coursePrice" : 0,
"coursetype" : "offline"
},
{
"courseId" : "57fc31f443fa1fe885d3ad64",
"userInfo" : [
{
"status" : "requested",
"firstName" : "asdasd",
"dob" : "2015-03-23T18:30:00.000Z",
"lastName" : "aasdasd",
"userId" : "RQ-WqEXBkjv5",
"eMail" : "asdasd#gmail.com"
}
],
"userCount" : 1,
"coursePrice" : 0,
"Name" : "Introduction to Haptics: Self-Paced",
"coursetype" : "offline"
}
],
"companyId" : ObjectId("54128cc57525614f6e3e710a"),
"createdDate" : ISODate("2015-03-23T11:26:29.027Z"),
"updatedDate" : ISODate("2015-03-24T15:00:33.248Z"),
"crmId" : ObjectId("660ab20bd4f9075d40a10d52"),
"urmId" : ObjectId("660ab20bd4f9075d40a10d52"),
"activeFlag" : 0,
"customCompanyCode" : "baa-106",
"status" : "Pending approval"}
From above document i have to get the users who have the status "Approved" in userInfo object which exists inside this document. I have created one query but this will output all the users who have different status.My query look like below,
db.clnTrainingRequest.find({companyId:ObjectId('54128cc57525614f6e3e710a'), "orderDetails.userInfo.status":{$in:['Approved']}}).toArray()
Please any one help me to sort out this issue
You can use aggregation. You need to $unwind orderDetails and the userInfo arrays and then use the $match to get users with status Approved
db.clnTrainingRequest.aggregate(
[ { "$match": { "companyId": ObjectId('54128cc57525614f6e3e710a') },
{ "$unwind": "$orderDetails" },
{ "$unwind": "$orderDetails.userInfo" },
{ "$match": { "orderDetails.userInfo.status": "Approved" }}
]
)