I have a collection in which subject and student are unique fields and I will be saving field called rank for that combination in particular date. So I want to group data by date based on that combination. I am using node js, mongodb. Could anyone answer my query.
Sample documents:
{ "_id" : "1", "createdDateStr" : "2018-09-30", "student" : ObjectId("5b9661a7ffc73a683d4c0ded"), "subject" : "physics", "rank" : 9 },
{ "_id" : 2, "createdDateStr" : "2018-09-26", "student" : ObjectId("5b9661a7ffc73a683d4c0ded"), "subject" : "physics", "rank" : 12 },
{ "_id" : 3, "createdDateStr" : "2018-09-12", "student" : ObjectId("5b9661a7ffc73a683d4c0ded"), "subject" : "physics", "rank" : 10 }
Expected Output:
{ "student" : ObjectId("5b9661a7ffc73a683d4c0ded"), "subject": "physics", "ranks": [{"createdDateStr" : "2018-09-12", "rank":10},{"createdDateStr" : "2018-09-26", "rank":12},{"createdDateStr" : "2018-09-30", "rank":9},] }
Related
I need to fetch data from from mongodb with respect to array elements which i will get in req.body
Description:
POSTMAN REQUEST there can be more than 10 mappedVendors also
{
"_id": "5e2554ec3405363bc4bf86c0",
"mappedVendors": [
"5e2555783405363bc4bf86c5",
"5e2555643405363bc4bf86c4",
"5e2555643405363bc4bf86c8"
]}
collection 1 : masters
"_id" : ObjectId("5e2554ec3405363bc4bf86c0"),
"mappedVendors" : [
ObjectId("5e2555783405363bc4bf86c5"),
ObjectId("5e2555643405363bc4bf86c4"),
ObjectId("5e2555643405363bc4bf86c9")
"phoneNo" : 9082625539,
"name" : "dealer 2",
"address" : "dealer address 2",
"depotCode" : "D3137",
"state" : "Tamilnadu",
"city" : "chennai",
NOTE: deatils of mappedVendors for e.g: ObjectId("5e2555643405363bc4bf86c4") is in the same masters collection
collection 2 : orders
"_id" : ObjectId("5e2848e309b2ad1b4c5d994c"),
"isApproved" : false,
"orderCreatedOn" : ISODate("2020-01-22T18:30:00Z"),
"vendorOrder" : [],
"frequency" : "WE",
"orderCreatedBy" : ObjectId("5e2555643405363bc4bf86c4"),
Collection 3: payments
"_id" : ObjectId("5dd78e6c8f2683327c1e26e1"),
"paymentOfTheDay" : 2200,
"outstanding" : 300,
"paymentDoneBy":ObjectId("5e2555643405363bc4bf86c4")
scenario is :
i will get mappedVendors array in req.body it can be many and with respect to each mappedVendor
i need to fetch these mentioned fields:"phoneNo","name","address","depotCode" from masters collection + "isApproved" from orders collection + "outstanding" from payments collection than i need to send all this **data in an array object ** to the frontend for each and every mappedVendors
"vendorListDetails":[{
"phoneNo","name","address","depotCode" ,"isApproved","outstanding"
}]
I have two case ,first case: where in my key has reference to the same collection ,is this correct way to reference ?,if yes ! how to de-ref/link in node js ?,if not how to reference fom the same collection
{
"_id" : ObjectId("53402597d852426020000002"),
"address" : {
"$ref" : "home_adress"
},
"contact" : "987654321",
"dob" : "01-01-1991",
"name" : "Tom Benzamin",
"Post_address" : {
"$ref" : "home_adress"
},
"home_adress":"Street 1, NY"}
case two: reference to a different collection(DBRefs),How to detect the reference and send the second query to fetch the referenced value ?
{
"_id" : ObjectId("53402597d852426020000002"),
"address" : {
"$ref" : "address_home",
"$id" : ObjectId("534009e4d852427820000002"),
"$db" : "ref"
},
"contact" : "987654321",
"dob" : "01-01-1991",
"name" : "Tom Benzamin"}
{
"_id" : ObjectId("534009e4d852427820000002"),
"building" : "22 A, Indiana Apt",
"pincode" : 123456,
"city" : "Los Angeles",
"state" : "California"}
I have a mongo database running in cmd.
There is a collection named usercollection.
And there is also some documents which I'm getting by typing in mongo shell:
db.usercollection.find().sort({score: -1}).pretty()
Output:
{
"_id" : ObjectId("57501b2d7ef17cd8c1bd63bc"),
"name" : "QWERTY",
"score" : "100",
"link" : "http://qwerty.com"
}
{
"_id" : ObjectId("5750199819433f80da88c847"),
"name" : "Adam Sandler",
"score" : "222",
"link" : "http://sandler.com"
}
{
"_id" : ObjectId("57501af57ef17cd8c1bd63bb"),
"name" : "gfhfghfghguy",
"score" : "5",
"link" : "http://dsad.com"
}
{
"_id" : ObjectId("57501bb47ef17cd8c1bd63bd"),
"name" : "Test name",
"score" : "50",
"link" : "http://fdsfds.cpm"
}
{
"_id" : ObjectId("574f4a9dab868fdc896d11b4"),
"name" : "Rich Man",
"score" : "500",
"link" : "http://jikl.me"
}
{
"_id" : ObjectId("574f50f1fbd256888ffa248d"),
"name" : "lfgfgff",
"score" : "9",
"link" : "http://oh.com"
}
{
"_id" : ObjectId("574f5324fbd256888ffa248e"),
"name" : "test name",
"score" : "9",
"link" : "http://dsad.com"
}
The problem is that I'm trying to sort these documents from one with highest score to one with lowest score and it's output is completely random(
I'm displaying that documents on the website using node.js and the result is the same as in the console.
Try with :
db.usercollection.find().sort({score: 'desc'}).pretty()
I have a mongodb collection data as per below;I want to group by EmployeedID( i.e 0001) and then sort(by age)
{
"_id" : ObjectId("54d0512191a4da7736e9db43"),
"EmployeeID" : "0001",
"Speciality" : "xxx",
"Code" : "P",
"Age" : 8
}
/* 1 */
{
"_id" : ObjectId("54d0512191a4da7736e9db44"),
"EmployeeID" : "0002",
"Speciality" : "yyyyy",
"Code" : "P",
"Age" : 6
}
/* 2 */
{
"_id" : ObjectId("54d0512191a4da7736e9db45"),
"EmployeeID" : "0001",
"Speciality" : "zzz",
"Code" : "P",
"Age" : 5
}
I know I can group using the following way.
collection.aggregate([
{$match:{"EmployeeId":0001}},
{$group:{"_id":"$EmployeeID",
"speciality":{$push:"$Speciality"},
"Code":{$push:"$Code"},
"Age":{$push:"$Age"}}}
])
But how can I using $sort here? SO my result can be something like below;
[{ "EmployeeID" : "0001",
"speciality" : [ "zzz","xxx"],
"Code" :[ "P","P"],
"Age" : [5,8]
}]
You can sort the document prior to the grouping stage:
collection.aggregate([
{$sort: {_id: -1}},
{$match:{"EmployeeId":0001}},
{$group:{"_id":"$EmployeeID",
"speciality":{$push:"$Speciality"},
"Code":{$push:"$Code"},
"Age":{$push:"$Age"}}}
])
Sorting prior to grouping may exceed mongo's memory when dealing with large collections. Fortunately, you can set allowDiskUse to true to allow mongo to write temporary files.
Given this Person collection:
{
"_id" : ObjectId("4f8e95a718bcv9c74da1e6511a"),
"name" : "John",
"hobbies" : [{
"id" : 001,
"name" : "reading",
"location" : "home"
},{
"id" : 002,
"name" : "sport",
"location" : "outside"
}]
}
and these new/edited Hobby objects:
{
"name" : "walking",
"location" : "outside"
}
and
{
"id" : 001,
"name" : "reading",
"location" : "outside"
}
If I know the Person that I want to manage, what is be the best way to upsert embedded objects?
Currently my approach is to find the Person object, make the required modifications to it in my code, and then save it back to the DB. This works. But I'd like to simplify and reduce the number of round trips to the database.