MongoDB Cross-Collection Query - mongodb

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

Related

MongoDB - Aggregate deep tree structures

I would like to extract tree graph from my database
This is my structure (which always is always a tree graph)
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b2"), "title" : "a", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b3") } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b3"), "title" : "b", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b4") } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b4"), "title" : "c", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b5") }, { "_id" : ObjectId("59490a250f86a4b4e0cb75b6") } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b5"), "title" : "d" }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b6"), "title" : "e", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b7") } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b7"), "title" : "f" }
I was trying to do something like this
db.test3.aggregate([{
$lookup: {
from: "test3",
localField: "_id",
foreignField: "_id",
as: "array"
}
}])
But then I just receive
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b2"), "title" : "a", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b2"), "title" : "a", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b3") } ] } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b3"), "title" : "b", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b3"), "title" : "b", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b4") } ] } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b4"), "title" : "c", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b4"), "title" : "c", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b5") }, { "_id" : ObjectId("59490a250f86a4b4e0cb75b6") } ] } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b5"), "title" : "d", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b5"), "title" : "d" } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b6"), "title" : "e", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b6"), "title" : "e", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b7") } ] } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b7"), "title" : "f", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b7"), "title" : "f" } ] }
but this doesnt seems to work as expected
According to description as mentioned in above question please try executing following aggregate query in MongoDB shell
db.test3.aggregate(
// Pipeline
[
// Stage 1
{
$unwind: {
path : "$array"
}
},
// Stage 2
{
$lookup: {
"from" : "mycoll",
"localField" : "array._id",
"foreignField" : "_id",
"as" : "arr"
}
}
]
);

Mongodb - Return all the associative documents with value for the key derived from another query

I have a document of following structure:
{
"Type" : "Request",
"Cat" : "A",
"ID" : 10
}
{
"Type" : "Processed",
"Cat" : "A",
"ID" : 10
}
{
"Type" : "Receieved",
"Cat" : "A",
"ID" : 10
}
{
"Type" : "Receieved",
"Cat" : "B",
"ID" : 11
}
{
"Type" : "Processed",
"Cat" : "C",
"ID" : 12
}
I want documents:
Those documents with Type: "Processed" and get its ID
And all the associated documents with the ID got from above (1st step).
I need the results to be like this:
{
"Type" : "Request"
"Cat" : "A"
"ID" : 10
}
{
"Type" : "Processed"
"Cat" : "A"
"ID" : 10
}
{
"Type" : "Receieved"
"Cat" : "A"
"ID" : 10
}
{
"Type" : "Processed"
"Cat" : "C"
"ID" : 12
}
Can someone help me on how to achieve this ? I used elemmatch under $match in aggregate - but its not working as expected.
You can try something like
db.collection.aggregate([
{$project : {
"ID":1,
"doc.Type" : "$Type",
"doc.Cat" : "$Cat",
"doc.ID" : "$ID"
}
}
{$group : {
_id : "$ID",
docs : {$push : doc}
}
},
{$match : {
"docs.Type":"Processed"
}
},
{$unwind : "$docs"},
{$project : {
_id : 0,
docs : 0,
"Type" : "$docs.Type",
"Cat" : "$docs.Cat",
"ID" : "$docs.ID"
}
}
])

MongoDB - query for a nested item inside a collection

i have a mongodb collection "result" with data like
{ "_id" : {
"user" : "Howard",
"friend" : "Sita"
},
"value" : {
"mutualFriend" :[ "Hanks", "Bikash", "Shyam", "Bakshi" ]
}
}
{ "_id" : {
"user" : "Shiva",
"friend" : "Tom"
},
"value" : {
"friendList" :[ "Hanks", " Tom", " Karma", " Hari", " Dinesh" ]
}
}
{ "_id" : {
"user" : "Hari",
"friend" : "Shiva"
},
"value" : {
"mutualFriend" :[ "Tom", "Karma", "Dinesh" ]
}
}
Now, here i want to query whole Document having value.mutualFriend. how can i get the result?
Expected Output
{ "_id" : {
"user" : "Howard",
"friend" : "Sita"
},
"value" : {
"mutualFriend" :[ "Hanks", "Bikash", "Shyam", "Bakshi" ]
}
}
{ "_id" : {
"user" : "Hari",
"friend" : "Shiva"
},
"value" : {
"mutualFriend" :[ "Tom", "Karma", "Dinesh" ]
}
}
i have large number of document in MongoDB collection, containing value.friendList and value.mutualFriend and then i want to find only documents with value.mutualFriend
db.collection.find({"value.mutualFriend.0" : { $exists : true }})
Its just make sure that the 0th element exists. you can customize your query over various array length.

Mongoose aggregation improvement

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")
}
}
}
]

MongoDB find documents if a property array doesn't contain an object

I have a list of documents like this.
[
{
"name" : "test",
"data" : [
{ "code" : "name", "value" : "Diego" },
{ "code" : "nick", "value" : "Darko" },
{ "code" : "special", "value" : true }
]
},
{
"name" : "another",
"data" : [
{ "code" : "name", "value" : "Antonio" },
{ "code" : "nick", "value" : "Tony" }
]
}
]
now I need to find all the documents that:
a) don't contain a "data" item with code "special"
OR
b) contains a "data" item with code "special" and value false
It's like I needed the opposite of $elemMatch or I'm missing something?
I'm assuming that you're inserting each document in your list of documents as a separate member of a collection test.
For a,
db.test.find({ "data.code" : { "$ne" : "special" } })
For b.,
db.test.find({ "data" : { "$elemMatch" : { "code" : "special", "value" : false } } })
Combining the two with $or,
db.test.find({ "$or" : [
{ "data.code" : { "$ne" : "special" } },
{ "data" : { "$elemMatch" : { "code" : "special", "value" : false } } }
] })
Hope this $nin will solve your issues.
I insertd your docs into "so" collection
db.so.find({}).pretty();
{
"_id" : ObjectId("5489cd4f4cb16307b808d4b2"),
"name" : "test",
"data" : [
{ "code" : "name",
"value" : "Diego"
},
{ "code" : "nick",
"value" : "Darko"
},
{ "code" : "special",
"value" : true
}
]
}
{
"_id" : ObjectId("5489cd674cb16307b808d4b3"),
"name" : "another",
"data" : [
{"code" : "name",
"value" : "Antonio"
},
{ "code" : "nick",
"value" : "Tony"
}
]
}
don't contain a "data" item with code "special"
> db.so.find({"data.code":{$nin:["special"]}}).pretty();
{
"_id" : ObjectId("5489cd674cb16307b808d4b3"),
"name" : "another",
"data" : [
{ "code" : "name",
"value" : "Antonio"
},
{ "code" : "nick",
"value" : "Tony"
}
]
}
contains a "data" item with code "special" and value false
db.so.find({$and:[{"data.code":"special"},{"data.value":false}]}).pretty();