Given this dataset and this mongodb, how to properly convert this aggregation into Mongoose?
I have included, the code using mongoose, which works but I want to know if this is the right way of doing it and that if this aggregation can be improved?
Thanks.
db.cars.aggregate(
//De-normalized the nested array of accounts
{"$unwind": "$accounts"},
//De-normalized the nested array of cars
{"$unwind": "$accounts.cars"},
//match carId to 3C
{"$match": {"accounts.cars.carId" : "3C"}},
//Project the accounts.cars object only
{"$project" : {"accounts.cars" : 1}}
).pretty();
The Mongoose version that I'm trying to improve:
Car.aggregate()
.unwind('accounts')
.unwind('accounts.cars')
.match({'accounts.cars.carId' : "3C"})
.project({"accounts.cars": 1, _id: 0})
.exec(function (err, carsObj) {});
and the dataset (cars):
{
"_id" : ObjectId("56223329b64f07a40ef1c15c"),
"username" : "john",
"email" : "john#john.com",
"accounts" : [
{
"_id" : ObjectId("56322329b61f07a40ef1c15d"),
"cars" : [
{
"carId" : "6A",
"_id" : ObjectId("56323329b64f07a40ef1c15e")
},
{
"carId" : "6B",
"_id" : ObjectId("56323329b64f07a40ef1c15e")
}
]
}
]
},
{
"_id" : ObjectId("56223125b64f07a40ef1c15c"),
"username" : "paul",
"email" : "paul#paul.com",
"accounts" : [
{
"_id" : ObjectId("5154729b61f07a40ef1c15d"),
"cars" : [
{
"carId" : "5B",
"_id" : ObjectId("56323329854f07a40ef1c15e")
}
]
},
{
"_id" : ObjectId("56322117b61f07a40ef1c15d"),
"cars" : [
{
"carId" : "6G",
"_id" : ObjectId("51212929b64f07a40ef1c15e")
},
{
"carId" : "3C",
"_id" : ObjectId("51273329b64f07a40ef1c15e")
},
{
"carId" : "4N",
"_id" : ObjectId("51241279b64f07a40ef1c15e")
}
]
}
]
}
What the aggregation returns is:
[
{ accounts:
{ cars:
{
"carId" : "3C",
"_id" : ObjectId("51273329b64f07a40ef1c15e")
}
}
}
]
Related
Assuming the following structure:
Assets
{
"_id" : LUUID("d34a3fed"),
"name" : "A",
"records" : [
LUUID("3627f3ac"),
LUUID("80e9d125"),
LUUID("4d5e8af5"),
LUUID("17593a39"),
}
Records
{
"_id" : LUUID("3627f3ac"),
"Fields" : [
{
"Name" : "foo",
"Value" : "bar",
}
],
}
My goal is to use a find() or aggregate() to cross-reference the two collections above. The two collections share the LUUID values.
{"records": "LUUID("3627f3ac")"}
{"_id": "LUUID("3627f3ac")"}
Ultimately retrieving the:
{"Fields.Name": "foo"}
name of the Records collection
Maybe something like this:
mongos> db.records.find()
{ "_id" : ObjectId("5ff8ccf9e0f1b975b90d7a86"), "fields" : [ { "name" : "foo", "value" : "bar" } ] }
{ "_id" : ObjectId("5ff8ccf9e0f1b975b90d7a87"), "fields" : [ { "name" : "foo2", "value" : "bar2" } ] }
{ "_id" : ObjectId("5ff8ccf9e0f1b975b90d7a88"), "fields" : [ { "name" : "foo3", "value" : "bar3" } ] }
mongos> db.assest.find()
{ "_id" : ObjectId("5ff8cd72e0f1b975b90d7a87"), "name" : "A", "records" : [ ObjectId("5ff8ccf9e0f1b975b90d7a86"), ObjectId("5ff8ccf9e0f1b975b90d7a87") ] }
mongos> db.assest.aggregate([ { $lookup:{ from:"records" , localField:"records" , foreignField:"_id" , as:"match" } } , {$unwind:"$match"} , {$unwind:"$match.fields"} ,{$project:{ "Fields_name":"$match.fields.name" ,_id:0}} ])
{ "Fields_name" : "foo" }
{ "Fields_name" : "foo2" }
mongos>
Playground
I'm struggling to identified duplicated elements in my MongoDB records, here is my problem :
I have a Mongo collection named "elements".
Example of a record in this collection :
{
"_id" : ObjectId("5d1b2204e851271e80c824b6"),
"name" : "A",
"items" : [
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d7"),
"_id" : ObjectId("5d1b2205e851271e80c82534")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d6"),
"_id" : ObjectId("5d1b2205e851271e80c82533")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d8"),
"_id" : ObjectId("5d1b2205e851271e80c82532")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d5"),
"_id" : ObjectId("5d1b3048e851271e80c826a5")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d5"),
"_id" : ObjectId("5d1b3048e851271e80c826ad")
}
]
}
I would like to identify records where the array "items" contains objects with the same "ref_id".
In my example we can see that the last two objects of the "items" array have the same "ref_id" : ObjectId("5d1b2204e851271e80c823d5").
I tried a bunch of aggregate function but unfortunately couldn't came out with a solution.
The following query can get us the expected output:
db.elements.aggregate([
{
$unwind:"$items"
},
{
$group:{
"_id":"$_id",
"root":{
$first:"$$ROOT"
},
"items":{
$push:"$items"
},
"distinctItems":{
$addToSet: "$items.ref_id"
}
}
},
{
$match:{
$expr:{
$ne:[
{
$size:"$items"
},
{
$size:"$distinctItems"
}
]
}
}
},
{
$addFields:{
"root.items":"$items"
}
},
{
$replaceRoot:{
"newRoot":"$root"
}
}
]).pretty()
Data set:
{
"_id" : ObjectId("5d1b2204e851271e80c824b6"),
"name" : "A",
"items" : [
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d7"),
"_id" : ObjectId("5d1b2205e851271e80c82534")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d6"),
"_id" : ObjectId("5d1b2205e851271e80c82533")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d8"),
"_id" : ObjectId("5d1b2205e851271e80c82532")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d5"),
"_id" : ObjectId("5d1b3048e851271e80c826a5")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d5"),
"_id" : ObjectId("5d1b3048e851271e80c826ad")
}
]
}
{
"_id" : ObjectId("5d654b9d7d0ab652c42315f2"),
"name" : "B",
"items" : [
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d7"),
"_id" : ObjectId("5d1b2205e851271e80c82534")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d6"),
"_id" : ObjectId("5d1b2205e851271e80c82533")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d8"),
"_id" : ObjectId("5d1b2205e851271e80c82532")
}
]
}
Output:
{
"_id" : ObjectId("5d1b2204e851271e80c824b6"),
"name" : "A",
"items" : [
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d7"),
"_id" : ObjectId("5d1b2205e851271e80c82534")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d6"),
"_id" : ObjectId("5d1b2205e851271e80c82533")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d8"),
"_id" : ObjectId("5d1b2205e851271e80c82532")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d5"),
"_id" : ObjectId("5d1b3048e851271e80c826a5")
},
{
"ref_id" : ObjectId("5d1b2204e851271e80c823d5"),
"_id" : ObjectId("5d1b3048e851271e80c826ad")
}
]
}
Explanation: We are populating an array of distinct ref_id from each document and matching if the size of the populated array is equal to the size of actual items array.
I have following collections:
Collection A
{
"_id" : ObjectId("5aaa3b170e26ed1eba223ba9"),
"name" : "A1",
"ref" : {
"$ref" : "B",
"$id" : ObjectId("5aaa33740e26ed1eba223ba1")
}
}
{
"_id" : ObjectId("5aaa3b170e26ed1eba223baa"),
"name" : "A2",
"ref" : {
"$ref" : "C",
"$id" : ObjectId("5aaa33740e26ed1eba223ba2")
}
}
Collection B
{
"_id" : ObjectId("5aaa33740e26ed1eba223ba1"),
"name" : "B1"
}
...
Collection C
{
"_id" : ObjectId("5aaa33740e26ed1eba223ba2"),
"name" : "C1"
}
...
It is posable to get folowing result?
{
"_id" : ObjectId("5aaa3b170e26ed1eba223ba9"),
"name" : "A1",
"result" : [
{
"name" : "B1"
}
]
}
{
"_id" : ObjectId("5aaa3b170e26ed1eba223baa"),
"name" : "A2",
"result" : [
{
"name" : "C1"
}
]
}
I tried it with $Project and §Lookups, unfortunately withou success.
Hire is example:
db.A.aggregate([
{$project: {
name : 1,
refId: {$arrayElemAt: [{$objectToArray:"$ref"},1]},
refCol: {$arrayElemAt: [{$objectToArray:"$ref"},0]},
}
},
{$lookup : {
from : "refCol.v",
localField : "refId.v",
foreignField : "_id",
as : "result"
}
},
{$project : {"result._id" : 0, refId : 0, refCol : 0}}
])
In this example I can't reference the "refCol.v" field in the $lookup function.
Have someone a tip or a better solution for me?
You can achieve this by populate() method:
const collectionA = require('../models/collectionA');
collectionA.find({}).populate({
path: 'collectionB',
select: {
'name': 1,
'_id': 0
}
})
}).then((data) => {
if (data) {
res.send(data);
}
}).catch((err) => {
res.send(err);
})
I willing to apply $filter in this collection to get only "Applied Mathematics" result only in array. I am providing input in form of array like this ["Applied Mathematics"] to below collection.
{
"_id" : ObjectId("58a95d81eead32e82932b535"),
"univisorEducation" : [
{
"educationLevel" : "BACHELOR",
"courseType" : "UNDERGRADUATE",
"year" : 2016,
"college" : ObjectId("58a936add48f2b502858a70d"),
"_id" : ObjectId("58a95eb2eead32e82932b541"),
"course" : [
"Anthropology",
"Biomedical Engineering",
"Applied Mathematics"
]
}
]
}
{
"_id" : ObjectId("58a9643deead32e82932b54b"),
"univisorEducation" : [
{
"educationLevel" : "DOCTORATE",
"courseType" : "GRADUATE",
"year" : 2020,
"college" : ObjectId("58a936afd48f2b502858b5e2"),
"_id" : ObjectId("58a96495eead32e82932b550"),
"course" : [
"Applied Mathematics",
"Applied Physics"
]
},
{
"educationLevel" : "MASTER",
"courseType" : "GRADUATE",
"year" : 2020,
"college" : ObjectId("58a936afd48f2b502858b9f7"),
"_id" : ObjectId("58a96495eead32e82932b54f"),
"course" : [
"Applied Mathematics"
]
}
]
}
For this purpose i am using a $filter in $project, below is my $filter code
$filter: {
input: "$univisorEducation",
as: "univisorEducation",
cond: {
$setIsSubset: ["$$univisorEducation.course", courses]
}
}
Issue is that i am only getting this result
{ "_id" : ObjectId("58a95d81eead32e82932b535"), "univisorEducation" : [ ] }
{
"_id" : ObjectId("58a9643deead32e82932b54b"),
"univisorEducation" : [
{
"educationLevel" : "MASTER",
"courseType" : "GRADUATE",
"year" : 2020,
"college" : ObjectId("58a936afd48f2b502858b9f7"),
"_id" : ObjectId("58a96495eead32e82932b54f"),
"course" : [
"Applied Mathematics"
]
}
]
}
The collection with "_id" : ObjectId("58a95d81eead32e82932b535") should have result with Applied Mathematics.
Change your $filter aggregation to below.
Compares courses array to univisorEducation.course and returns true there is match univisorEducation.course or false otherwise.
$filter: {
input: "$univisorEducation",
as: "univisorEducation",
cond: {
$ne:[{$setIntersection: [courses, "$$univisorEducation.course" ]}, []]
}
}
I have mongo documents in the following format. I want to get the devices_ids for each unique phone_numbers but my mongo query is not giving proper result.
Can anyone point out my problem ?
{
"_id" : ObjectId("56cf21562e7b232d022f334e871"),
"uid" : 5,
"device_id" : "352136234234325",
"name" : "user1",
"email" : ["user1#mail.com" ],
"phone_number" : [
"+919890273451"
]
}
{
"_id" : ObjectId("56cf21562e7b2d032422f334e872"),
"uid" : 15,
"device_id" : "3521360123444",
"name" : "user1",
"email" : [ "user1#mail.com"],
"phone_number" : [
"+919890273451"
]
}
{
"_id" : ObjectId("56cf21562342e7b2d022f334e873"),
"uid" : 51,
"device_id" : "352136067208559",
"name" : "user1",
"email" : [ "user1#mail.com"],
"phone_number" : [
"+919890273451"
]
}
My expected output is
{
"phone_number" : "+919890273451",
device_ids : ["352136067208559","3521360123444","352136234234325"]}
}
I have tried this query:
db.contact.aggregate([{
$unwind: "$phone_number"
},
{$group: {"_id":"$phone_number"},
device_ids: { $push: { user: "$device_id"} }
}
], {
allowDiskUse:true,
cursor:{}
});
when using $push you don't need to specify touple name - just push plain value.
Please see below:
db.coll.aggregate([{
$unwind : "$phone_number"
}, {
$group : {
_id : "$phone_number",
device_ids : {
$addToSet : "$device_id"
}
}
}
])