Mongoose/MongoDB sum up fields of an array for subdocuments - node.js

I am rather new to MongoDB/Mongoose and I have no idea how I am supposed to get the result I am looking for. Basically I have a document like this:
{
"playerId" : "",
"sessions" : [
{
"start" : "",
"end" : "",
"join" : "",
"leave" : "",
"rounds" : [,
{
"name" : "roundName",
"weapons" : [
{
"name" : "weaponName",
"kills" : 1,
"assits" : 1,
"deaths" : 1,
"shots" : 1,
"headshots" : 1
},
{
"name" : "weaponName",
"kills" : 1,
"assits" : 1,
"deaths" : 1,
"shots" : 1,
"headshots" : 1
}
]
},
{
"name" : "roundName",
"weapons" : [
{
"name" : "weaponName",
"kills" : 1,
"assits" : 1,
"deaths" : 1,
"shots" : 1,
"headshots" : 1
},
{
"name" : "weaponName",
"kills" : 1,
"assits" : 1,
"deaths" : 1,
"shots" : 1,
"headshots" : 1
}
]
},
{
"name" : "roundName",
"weapons" : [
{
"name" : "weaponName",
"kills" : 1,
"assits" : 1,
"deaths" : 1,
"shots" : 1,
"headshots" : 1
},
{
"name" : "weaponName",
"kills" : 1,
"assits" : 1,
"deaths" : 1,
"shots" : 1,
"headshots" : 1
}
]
}
]
}
]
}
What I want is a query that adds fields to each round (kills, deaths, assists, headshots, shots) which contain the sum of all their weapons. The same should happen for the entire session if possible. Any help would be greatly appreciated!

the following should give you list of stats grouping by session->rounds:
the assumption here is 1) name of the collection is shootingGame 2) name of the fields are $sessions.Rounds.Weapons
db.shootingGame.aggregate(
[
{$group: {
_id: "$sessions.Rounds",
"sumkills": {$sum: "$sessions.Rounds.Weapons.kills" },
"sumasists": {$sum: "$sessions.Rounds.Weapons.assists" },
"sumdeaths": {$sum: "$sessions.Rounds.Weapons.deaths" },
"sumheadshots": {$sum: "$sessions.Rounds.Weapons.headshots" },
"sumshots": {$sum: "$sessions.Rounds.Weapons.shots" }
}}
]
)
But for further grouping per session, you have to store this result into another document and run another $sum group per round

Related

aggregate in Mongodb driver(npm) result is different from robo3T result

its my aggregate in robo3t and node app
db.getCollection('roles').aggregate([
{$match:{"_id" : ObjectId("63c8ad93eb3b2d975834a6d0")}},
{
$lookup: {
from: 'modules',
localField: 'moduleAccess.moduleId',
foreignField: '_id',
as: 'moduleData'
}
},
{
$unwind:'$moduleData'
}
]).toArray()
my result in robo3t and nodejs is :
robo3t :
/* 1 */
[
{
"_id" : ObjectId("63c8ad93eb3b2d975834a6d0"),
"roleName" : "SuperUser",
"activeStatus" : "Active",
"moduleAccess" : [
{
"moduleId" : ObjectId("63ca80563eea1005982f82d4"),
"view" : 1,
"create" : 1,
"update" : 1,
"delete" : 1
},
{
"moduleId" : ObjectId("63ca80a13eea1005982f82f8"),
"view" : 1,
"create" : 1,
"update" : 1,
"delete" : 1
},
{
"moduleId" : ObjectId("63ce1de83eea1005982f8f4f"),
"view" : 1,
"create" : 1,
"update" : 1,
"delete" : 1
}
],
"moduleData" : {
"_id" : ObjectId("63ca80563eea1005982f82d4"),
"displayName" : "Module List",
"moduleName" : "moduleList",
"moduleIcon" : "fa fa-circle",
"moduleLevel" : 1,
"moduleParent" : ObjectId("63ca80a13eea1005982f82f8"),
"order" : 0,
"Active" : "Active"
}
},
{
"_id" : ObjectId("63c8ad93eb3b2d975834a6d0"),
"roleName" : "SuperUser",
"activeStatus" : "Active",
"moduleAccess" : [
{
"moduleId" : ObjectId("63ca80563eea1005982f82d4"),
"view" : 1,
"create" : 1,
"update" : 1,
"delete" : 1
},
{
"moduleId" : ObjectId("63ca80a13eea1005982f82f8"),
"view" : 1,
"create" : 1,
"update" : 1,
"delete" : 1
},
{
"moduleId" : ObjectId("63ce1de83eea1005982f8f4f"),
"view" : 1,
"create" : 1,
"update" : 1,
"delete" : 1
}
],
"moduleData" : {
"_id" : ObjectId("63ca80a13eea1005982f82f8"),
"displayName" : "Master App",
"moduleName" : "masterApp",
"moduleIcon" : "fa fa-circle",
"moduleLevel" : 0,
"moduleParent" : "",
"order" : 0,
"Active" : "Active"
}
},
{
"_id" : ObjectId("63c8ad93eb3b2d975834a6d0"),
"roleName" : "SuperUser",
"activeStatus" : "Active",
"moduleAccess" : [
{
"moduleId" : ObjectId("63ca80563eea1005982f82d4"),
"view" : 1,
"create" : 1,
"update" : 1,
"delete" : 1
},
{
"moduleId" : ObjectId("63ca80a13eea1005982f82f8"),
"view" : 1,
"create" : 1,
"update" : 1,
"delete" : 1
},
{
"moduleId" : ObjectId("63ce1de83eea1005982f8f4f"),
"view" : 1,
"create" : 1,
"update" : 1,
"delete" : 1
}
],
"moduleData" : {
"_id" : ObjectId("63ce1de83eea1005982f8f4f"),
"displayName" : "Role",
"moduleName" : "role",
"moduleIcon" : "fa fa-circle",
"moduleLevel" : 1,
"moduleParent" : ObjectId("63ca80a13eea1005982f82f8"),
"order" : 1,
"Active" : "Active"
}
}
]
in robo3t result is answer as expected. its correct answer
nodejs result :
{
_id: new ObjectId("63c8ad93eb3b2d975834a6d0"),
roleName: 'SuperUser',
activeStatus: 'Active',
moduleAccess: [
{
moduleId: new ObjectId("63ca80563eea1005982f82d4"),
view: 1,
create: 1,
update: 1,
delete: 1
},
{
moduleId: new ObjectId("63ca80a13eea1005982f82f8"),
view: 1,
create: 1,
update: 1,
delete: 1
},
{
moduleId: new ObjectId("63ce1de83eea1005982f8f4f"),
view: 1,
create: 1,
update: 1,
delete: 1
}
],
moduleData: {
_id: new ObjectId("63ca80563eea1005982f82d4"),
displayName: 'Module List',
moduleName: 'moduleList',
moduleIcon: 'fa fa-circle',
moduleLevel: 1,
moduleParent: new ObjectId("63ca80a13eea1005982f82f8"),
order: 0,
Active: 'Active'
}
}
in nodejs only unwidn 1 data
what i do wrong with my config?
mongodb(npm) : 4.13.0
mongodb version : 5.0.7
node version : 16.14.2
please help to configuration my app the output from node js can be the same as robo3t

Mongodb Node.js $lookup with date and $match

Can anyone help I am using $lookup for join in MongoDB to get all orders where the date is between date "X" and date "Y" and chef_id is "P". something is wrong in "date" part which is not giving data on the dates. but when I use it in single Find query it works fine and gives data between the dates. but it does not give data when I applied with $lookup for join.
Here is my query
Order.aggregate([{
"$lookup": {
"localField": "user_id",
"from": "users",
"foreignField": "_id",
"as": "order_data"
}
},
{
"$match": {
"$and": [
{ "chef_id": mongoose.Types.ObjectId(req.body.chef_id) },
{ "booking_datetime": { $gte: start_time,
$lte: end_time } }
]
}
}
], function(err, gettt) {
if (err) {
res.json({ 'message': "Error", 'status': false, 'data': err });
return false;
} else {
if (gettt.length != 0) {
res.json({ 'message': "Orders Data", 'status': true, 'data': gettt });
} else {
res.json({ 'message': "No Orders for this date", 'status': false, 'data': gettt });
}
}
});
Can anyone help me out.
my collection is here "Order" collection
{
"_id" : ObjectId("5a4256cc3f76bc45065021fc"),
"order_status" : 2,
"total_order_amount" : "160",
"booking_datetime" : ISODate("2017-12-29T23:24:00.000Z"),
"customer_address" : "121/161, South Extension part",
"user_id" : ObjectId("5a3cb4a8a188f2074714f1de"),
"chef_id" : ObjectId("5a390b07f0b3563db59cb3ca"),
"updated_at" : ISODate("2017-12-26T14:03:56.742Z"),
"created_at" : ISODate("2017-12-26T14:03:56.342Z"),
"products" : [
{
"product_id" : "5a3a50fcefc0c972377c3012",
"product_name" : "sweet corn",
"quantity" : "12",
"_id" : ObjectId("5a4256cc3f76bc45065021fd"),
"updated_at" : ISODate("2017-12-26T14:03:56.736Z"),
"created_at" : ISODate("2017-12-26T14:03:56.736Z")
},
{
"product_id" : "5a3a5119efc0c972377c3013",
"product_name" : "chilly paneer",
"quantity" : "10",
"_id" : ObjectId("5a4256cc3f76bc45065021fe"),
"updated_at" : ISODate("2017-12-26T14:03:56.736Z"),
"created_at" : ISODate("2017-12-26T14:03:56.736Z")
},
{
"product_id" : "5a3a512cefc0c972377c3014",
"product_name" : "Gulab jamun",
"quantity" : "20",
"_id" : ObjectId("5a4256cc3f76bc45065021ff"),
"updated_at" : ISODate("2017-12-26T14:03:56.736Z"),
"created_at" : ISODate("2017-12-26T14:03:56.736Z")
},
{
"product_id" : "5a3a50fcefc0c972377c3012",
"product_name" : "ali baba",
"quantity" : "56",
"_id" : ObjectId("5a4256cc3f76bc4506502200"),
"updated_at" : ISODate("2017-12-26T14:03:56.736Z"),
"created_at" : ISODate("2017-12-26T14:03:56.736Z")
}
],
"__v" : 1
and here is my User Collection
{
"_id" : ObjectId("5a623f67eaa08537fe0dba02"),
"salt" : "73824ba53291740e15d26c300c997ce1436ac678299101171af74f4980433285",
"hash" : "8f78291ac737dac15f59f5438033a61de75282a3c671a8d0231406a8374adec140b4cb2dd30b852f05241c6f9900443906fafec22ad58c983dacaed8f9ef4f9039e72b748d9c63d924239aa40372923d824a9cc796079556c8bc5eb0b0f6b17e7fd4c35b8780c870d1b4b819e641e56ce2f88fb0a7fdfbfd91d15921e9b7441a7051523903b43b930f56057852e41ffdbdc044cc09b14ebaac77940576b483d58ff1e18c381d40a143abcd1a180ca208aac6a13eb5c819b97e7e5753bd6fc40fcc1e19b55cb816879b3fedbf187110e84149bad0918672bd2de49bc323a32f04dd0e55aded9a0157fd5eea7db645303eb4cf461e47ca905e1f196618814b88421a3cab9463dac01d5bf6aebcace6e4b1215c3cf07aaae1cac07c94dc28432d223407778f4c6b12b089e09d56a59b1f00084c727f06247c1799c1a8616c74693e2d7057a5026e3c02b9ef73bf867873508575a33fc1e956bd3c704c54e6cc38ffb22e7a04ade70db134ec87e9ed3f43a7273db115127470f8ca5d8def49ba47fe7852cdf0cbd3140b19d5fe358d29eb84519365eea6353fa34c7a6757fbd9ec2ba93eca802f21944da58cd72b5d0d7000f9fd6f231f0668b7e621117a18fcedf977515e181325a9210380e01892891fcc420a67cb5246688eb6e577fccb6d41e719b426fa20c4689af9a9485d0ae0cf026845de8b4f12c7277b9cc506b5e29224",
"email" : "eduardo.llano#geocampo.co",
"firstname" : "Pedro",
"lastname" : "Peez",
"dob" : "1980-01-19",
"phone" : "3185311158",
"gender" : "male",
"latitude" : "4.6936225",
"longitude" : "-74.0730777",
"address" : "Bogota",
"divice_token" : "dcd8cf3ceefc39b8",
"prossing_form" : "1",
"status" : true,
"role" : "chef",
"updated_at" : ISODate("2018-02-01T16:09:27.465Z"),
"created_at" : ISODate("2018-01-19T18:56:39.070Z"),
"products" : [
{
"product_name" : "Producto 1",
"product_price" : "100",
"discount" : "10",
"product_ingredients" : "Pepper",
"product_description" : "Nsjdjd jsjdjdjx",
"minimum_order" : "2",
"tags" : "Indian food",
"status" : "1",
"product_image0" : "https://s3-us-west-2.amazonaws.com/rafahoproject/a47df980-7221-4fde-97cd-977cb3dd1dcf.jpg",
"product_image1" : "https://s3-us-west-2.amazonaws.com/rafahoproject/3a282cae-028c-4d4a-9a22-4c2c43d440f1.jpg",
"product_image2" : "https://s3-us-west-2.amazonaws.com/rafahoproject/1ab4373e-ec63-43fe-9b94-7b85d289c7f9.jpg",
"_id" : ObjectId("5a6602aa135fae732d5ce3d7")
},
{
"product_name" : "Bandeja Paisa",
"product_price" : 100,
"discount" : 20,
"cuisine" : "Continental",
"minimum_order" : 5,
"cooking_time_at_chef_place" : 8,
"cooking_time_at_user_home" : 10,
"tags" : "Bandeja-Paisa,Bandeja,Paisa",
"status" : "1",
"product_image0" : "https://s3-us-west-2.amazonaws.com/rafahoproject/0252161e-0e2a-4682-b7ab-0dce355be794.jpg",
"_id" : ObjectId("5a71d268947536411def9b11")
},
{
"product_name" : "Fritanga",
"product_price" : 200,
"discount" : 30,
"cuisine" : "continental",
"minimum_order" : 6,
"cooking_time_at_chef_place" : 3,
"cooking_time_at_user_home" : 5,
"tags" : "Fritanga,platter-of-grilled-meats",
"status" : "1",
"product_image0" : "https://s3-us-west-2.amazonaws.com/rafahoproject/14c39e2f-4d02-4cd1-8aa7-2f7179b5ea0c.jpg",
"_id" : ObjectId("5a71d3c6947536411def9b12")
},
{
"product_name" : "Dominican Sancocho",
"product_price" : 300,
"discount" : 50,
"cuisine" : "continental",
"minimum_order" : 5,
"cooking_time_at_chef_place" : 1,
"cooking_time_at_user_home" : 2,
"tags" : "Dominican-Sancocho,Dominican,Sancocho",
"status" : "1",
"product_image0" : "https://s3-us-west-2.amazonaws.com/rafahoproject/40a701ff-f868-492d-bee1-d65004fff024.jpg",
"_id" : ObjectId("5a71d4da947536411def9b13")
},
{
"product_name" : "Sudado de Pollo",
"product_price" : 60,
"discount" : 10,
"cuisine" : "Continental",
"minimum_order" : 3,
"cooking_time_at_chef_place" : 3,
"cooking_time_at_user_home" : 2,
"tags" : "Sudado-de-Pollo,Sudado,de-Pollo",
"status" : "1",
"product_image0" : "https://s3-us-west-2.amazonaws.com/rafahoproject/544c2ceb-839b-4263-b482-e2262c228948.jpg",
"_id" : ObjectId("5a71d6e9947536411def9b14")
}
],
"loc" : {
"coordinates" : [
-74.0730777,
4.6936225
],
"type" : "Point"
},
"__v" : 9,
}
Your syntax looks good. I still suspect there is some data type mismatch or erroneous format. I have a similar aggregate which matches a date range, and I use new Date() to convert my strings to date.
if (typeof dateBeginning === "string" && dateBeginning != "" && typeof dateEnding === "string" && dateEnding != "" && dateEnding >= dateBeginning) {
query.date = { $gte: new Date(dateBeginning), $lte: new Date(dateEnding) };
}
Here is an SO question: Date query with ISODate in mongodb doesn't seem to work
which was resolved this way.
Also, Veeram is correct; you should put your $match first. That way, MongoDb will use any indexes on the match fields that are available.

Getting the index of an element in a mongodb subdocument which is an array of objects

I have a mongodb sample object like this,
{
"_id" : ObjectId("5937cbec0de02a0004cb57b3"),
"usernameAsEntered" : "logan",
"platform" : "ios",
"lastName" : "Jackman",
"firstName" : "Hugh",
"androidDevices" : [],
"iosDevices" : [],
"isAgeRestrictedCategories" : [],
"role" : "celeb",
"updatedAt" : ISODate("2017-09-19T16:36:11.356Z"),
"createdAt" : ISODate("2017-06-07T09:48:28.269Z"),
"lastUpdated" : ISODate("2017-06-09T10:36:59.448Z"),
"active" : false,
"birthday" : null,
"notificationCount" : 0,
"points" : 0,
"topicLocked" : [
"msfk",
"flirty",
"dirty",
"mahesh_test"
],
"hashfriends" : [],
"blockedList" : [],
"verifiedCeleb" : true,
"celebParticipants" : [
{
"user" : ObjectId("57ab51940a993203009d8506"),
"lastPlayed" : ISODate("2017-08-28T18:01:07.763Z"),
"_id" : ObjectId("59897f4b55acb50010f80741"),
"totalQuizAnswered" : 10,
"scoreInPercentage" : 90,
"score" : 9
},
{
"user" : ObjectId("577e223df34e570300de1872"),
"lastPlayed" : ISODate("2017-09-18T08:52:33.872Z"),
"_id" : ObjectId("5996d7a01e146200104fc7da"),
"totalQuizAnswered" : 10,
"scoreInPercentage" : 90,
"score" : 9
},
{
"user" : ObjectId("59bf6d53373af70011475b6d"),
"lastPlayed" : ISODate("2017-09-18T07:55:42.794Z"),
"_id" : ObjectId("59bf7ba5373af70011475b9d"),
"totalQuizAnswered" : 10,
"scoreInPercentage" : 80,
"score" : 8
},
{
"user" : ObjectId("5979f782be0970001abdc93b"),
"lastPlayed" : ISODate("2017-08-29T15:51:37.758Z"),
"_id" : ObjectId("59a584bba8ed1400100b8b1d"),
"totalQuizAnswered" : 7,
"scoreInPercentage" : 100,
"score" : 7
},
{
"user" : ObjectId("5755981ae2565d0300be2b4d"),
"lastPlayed" : ISODate("2017-08-25T03:27:38.653Z"),
"_id" : ObjectId("5988924e809e050010bf7011"),
"totalQuizAnswered" : 10,
"scoreInPercentage" : 70,
"score" : 7
},
{
"user" : ObjectId("571759ac1f86f003002dacfa"),
"lastPlayed" : ISODate("2017-09-05T09:01:56.949Z"),
"_id" : ObjectId("59ae677aaf24fa001083eeb3"),
"totalQuizAnswered" : 10,
"scoreInPercentage" : 60,
"score" : 6
},
{
"user" : ObjectId("599a88a41e17140010eca45a"),
"lastPlayed" : ISODate("2017-08-22T09:25:29.592Z"),
"_id" : ObjectId("599bf8184ff8d70010ef3167"),
"totalQuizAnswered" : 10,
"scoreInPercentage" : 40,
"score" : 4
},
{
"user" : ObjectId("58e72582e8b311000448ac22"),
"lastPlayed" : ISODate("2017-08-07T18:40:13.364Z"),
"_id" : ObjectId("5988b39555acb50010f806f1"),
"totalQuizAnswered" : 5,
"scoreInPercentage" : 60,
"score" : 3
},
{
"user" : ObjectId("58e72580e8b311000448ac1d"),
"lastPlayed" : ISODate("2017-08-07T18:10:20.652Z"),
"_id" : ObjectId("598899c1809e050010bf7017"),
"totalQuizAnswered" : 7,
"scoreInPercentage" : 43,
"score" : 3
},
{
"user" : ObjectId("5979f33abe0970001abdc91d"),
"lastPlayed" : ISODate("2017-08-29T07:15:35.251Z"),
"_id" : ObjectId("599b45cf4ff8d70010ef311c"),
"totalQuizAnswered" : 2,
"scoreInPercentage" : 100,
"score" : 2
},
{
"user" : ObjectId("59885cda809e050010bf6f99"),
"lastPlayed" : ISODate("2017-08-07T17:33:21.426Z"),
"_id" : ObjectId("59889921809e050010bf7014"),
"totalQuizAnswered" : 2,
"scoreInPercentage" : 100,
"score" : 2
},
{
"user" : ObjectId("59bf75c7373af70011475b91"),
"lastPlayed" : ISODate("2017-09-19T16:36:11.336Z"),
"_id" : ObjectId("59c1469c49e8650011ec8655"),
"totalQuizAnswered" : 7,
"scoreInPercentage" : 29,
"score" : 2
},
{
"user" : ObjectId("58e72580e8b311000448ac1e"),
"lastPlayed" : ISODate("2017-08-07T11:44:45.299Z"),
"_id" : ObjectId("59885299809e050010bf6f78"),
"totalQuizAnswered" : 2,
"scoreInPercentage" : 50,
"score" : 1
},
{
"user" : ObjectId("59bf49b6373af70011475aa6"),
"lastPlayed" : ISODate("2017-09-18T04:22:58.904Z"),
"_id" : ObjectId("59bf4a22373af70011475aab"),
"totalQuizAnswered" : 1,
"scoreInPercentage" : 100,
"score" : 1
},
{
"user" : ObjectId("5980872c1ab7740010e1982f"),
"lastPlayed" : ISODate("2017-08-01T13:56:44.261Z"),
"_id" : ObjectId("5980889c1ab7740010e1983e"),
"totalQuizAnswered" : 1,
"scoreInPercentage" : 100,
"score" : 1
},
{
"user" : ObjectId("59bb532f7b920a0011415034"),
"lastPlayed" : ISODate("2017-09-15T04:15:29.500Z"),
"_id" : ObjectId("59bb53e17b920a001141503b"),
"totalQuizAnswered" : 1,
"scoreInPercentage" : 0,
"score" : 0
},
{
"user" : ObjectId("59bb62c57b920a001141509a"),
"lastPlayed" : ISODate("2017-09-15T05:23:10.433Z"),
"_id" : ObjectId("59bb63be7b920a001141509d"),
"totalQuizAnswered" : 1,
"scoreInPercentage" : 0,
"score" : 0
}
],
"friends" : [],
"meta" : {
"totalInvites" : 0,
"totalFriends" : 0
},
"isDeleted" : false,
"__v" : 298,
"avatarUrl" : "https://1.soompi.io/wp-content/uploads/2015/10/hugh-jackman.jpg",
"lastActivity" : null,
"lastNudged" : ISODate("2016-04-25T06:46:38.267Z")
}
The celebParticipants array might have tens of thousands of objects being stored.
If I want to fetch the index of say user A from the celebParticipants array of User B, What would be the best way to achieve it using just the user parameter i.e, the ObjectId which is also a foreign key reference to the same collection as above.
I tried using something like this,
let index = await models.User.aggregate([
{'$match': {_id: mongoose.Schema.Types.ObjectId(req.params.id)}},
{
'$project':
{
'index': { '$indexOfArray': [ "$celebParticipants",
//can't just put in userId
mongoose.Schema.Types.ObjectId(userId)] },
}
}
]);
But this would not work, since user is a part of the object and the array isn't an array of just user ids. Is there a way to achieve this other than to fetch all the data and later iterating over it, or to use mapreduce.
Any help would be appreciated.
This works for me outside of mongoose; try it on for size. Note the addition of ._id because you need to match on the subfield celebParticipants._id not the array:
db.foo.aggregate([
{$project: {"idx": {"$indexOfArray": ["$celebParticipants._id", your mongoose objectid thing]}}}
]);

Get info from another collection based of _id and merge - MongoDB

I have two collections. Lets call one baskets and the other one fruits.
In baskets we have the following document:
[{
basket_name: "John's Basket",
items_in_basket: [
{
fruit_id: 1,
comment: "Delicious!"
},
{
fruit_id: 2,
comment: "I did not like this"
}
]
}]
And in fruits we have the following documents:
[{
_id: 1,
fruit_name: "Strawberry",
color: "Red"
},
{
_id: 2,
fruit_name: "Watermelon",
color: "Green"
}]
How do I get information on each fruit in John's Basket?
The result should look like this:
[{
fruit_id: 1,
comment: "Delicious!",
fruit_name: "Strawberry",
color: "Red"
},
{
fruit_id: 2,
comment: "I did not like this",
fruit_name: "Watermelon",
color: "Green"
}]
There's no "join" in MongoDB. You either could:
consider using a MapReduce function to create a new structure that contains the merged data
write the code necessary to fetch each fruit instance on demand and merge it in your client code with a basket document.
denormalize the data and include the details for each fruit in the basket document. This poses it's own set of issues as data is duplicated and updates to a particular fruit would then need to be made to every usage in the collection.
Both have their pros and cons.
You might find this Q/A helpful, and also this documentation for MongoDB.
this is no longer true.
Since version 3.2, MongoDB added the $lookup command.
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/
db.orders.insert([
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
{ "_id" : 3 }
])
db.inventory.insert([
{ "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 },
{ "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 },
{ "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 },
{ "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 },
{ "_id" : 5, "sku" : null, description: "Incomplete" },
{ "_id" : 6 }
])
db.orders.aggregate([
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
])
returns:
{
"_id" : 1,
"item" : "almonds",
"price" : 12,
"quantity" : 2,
"inventory_docs" : [
{ "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 }
]
}
{
"_id" : 2,
"item" : "pecans",
"price" : 20,
"quantity" : 1,
"inventory_docs" : [
{ "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 }
]
}
{
"_id" : 3,
"inventory_docs" : [
{ "_id" : 5, "sku" : null, "description" : "Incomplete" },
{ "_id" : 6 }
]
}

Mongo geospatial index options

In the mongo geospatial index documentation there is a parameter called "distanceMultiplier", but there's no explanation.
Anyone know what it does?
It scales the distance returned as "dis" in the result:
> db.runCommand({geoNear: 'places', near: [50, 50]})
{
"ns" : "geotest.places",
"near" : "1100110000001111110000001111110000001111110000001111",
"results" : [
{
"dis" : 7.853626559350774,
"obj" : {
"_id" : ObjectId("4d4b20890da611546bcbdf96"),
"loc" : {
"lat" : 42.739037,
"long" : 52.992964
},
"category" : [
"1"
]
}
},
> db.runCommand({geoNear: 'places', near: [50, 50], distanceMultiplier: 4})
{
"ns" : "geotest.places",
"near" : "1100110000001111110000001111110000001111110000001111",
"results" : [
{
"dis" : 31.414506237403096,
"obj" : {
"_id" : ObjectId("4d4b20890da611546bcbdf96"),
"loc" : {
"lat" : 42.739037,
"long" : 52.992964
},
"category" : [
"1"
]
}
},

Resources