Query for deeply nested array in mongodb - arrays

How do I query a deeply nested docs like show in the below image.
Here columns is an array of unknown size. Each element in the column contains a record that is again an array. Each element of the record array contains an array called fields. Each entry in the field contains 2 keys called name and value.
I'm querying the name of the innermost array (in fields array). I couldn't go above level 1 order of nesting.
JSON doc of the above image
"data" : {
"columns" : [
{
"name" : "styleId",
"record" : [
{
"fname" : "column_mapping",
"_id" : ObjectId("5ba488c79dc6d62c90257752"),
"fields" : [
{
"name" : "column_mapping_form",
"value" : "styleId"
}
],
"rules" : [
[
]
]
}
]
},
{
"name" : "vendorArticleNumber",
"record" : [
{
"fname" : "column_mapping",
"_id" : ObjectId("5ba488c79dc6d62c90257753"),
"fields" : [
{
"name" : "column_mapping_form",
"value" : "vendorArticleNumber"
}
],
"rules" : [
[
]
]
}
]
},
{
"name" : "vendorArticleName",
"record" : [
{
"fname" : "column_mapping",
"_id" : ObjectId("5ba488c79dc6d62c90257754"),
"fields" : [
{
"name" : "column_mapping_form",
"value" : "vendorArticleName"
}
],
"rules" : [
[
]
]
}
]
}
}
What can be the solutions if such kind of heaving nesting is there?

db.collection.find("data.columns.record.fields.name" : "column_mapping_form")
will match all documents where there is at least one element of columns has at least one record with at least one field where name is "column_mapping_form".
https://docs.mongodb.com/manual/tutorial/query-array-of-documents/ has very good explanation, examples, and interactive shell to play with.

Related

Compare array within objects in an array of the same Mongo document

I have a Mongo database with multiple documents and they all contain 2 Items for in store and online locations. The difference will be that attributes within the items object may differ. I am trying to capture all documents that have differing attributes between the object items with the array.
I've tried using $expr and comparing the position of the elements such as "Items.0.Attributes" == "Items.1.Attributes" but had no luck.
{
"ID" : "123456789",
"Items" : [
{
"ItemDept" : "softLines",
"ProductId" : {
"Name" : "shirts",
"_id" : "12345Shirts"
},
"Attributes" : [ "blue","small","mens"],
"Season" : "Summer"
"Location":"online"
}
,
{
"ItemDept" : "softlines",
"ProductId" : {
"Name" : "shirts",
"_id" : "12345Shirts")
},
"Attributes" : [ "blue","small","women"],
"Season" : "Summer"
"Location":"stores"
}
]
}
If I understand what you want to find/output, here's one way you could do it.
db.collection.find({
"$expr": {
"$not": {
"$setEquals": [
{"$first": "$Items.Attributes"},
{"$last": "$Items.Attributes"}
]
}
}
})
Try it on mongoplayground.net.

How to group records based on array elements using MongoDB

I have a data collection which contains a set of records in the following format.
{
"_id" : 22,
"title" : "3D User Interfaces with Java 3D",
"isbn" : "1884777902",
"pageCount" : 520,
"publishedDate" : ISODate("2000-08-01T07:00:00Z"),
"thumbnailUrl" : "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/barrilleaux.jpg",
"longDescription" : "Description",
"status" : "PUBLISH",
"authors" : [
"Jon Barrilleaux"
],
"categories" : [
"Java",
"Computer Graphics"
]
},
{
"_id" : 23,
"title" : "Specification by Example",
"isbn" : "1617290084",
"pageCount" : 0,
"publishedDate" : ISODate("2011-06-03T07:00:00Z"),
"thumbnailUrl" : "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/adzic.jpg",
"status" : "PUBLISH",
"authors" : [
"Gojko Adzic"
],
"categories" : [
"Software Engineering"
]
}
Please note that the 'categories' is an array.
I want to count the published books for each category. I tried the following solution, but it treated the entire array as one group.
db.books.aggregate([
{
$group:{_id:"$categories", total:{$sum:1}}
}
])
Instead of so, I want to count the number of records for each individual category value inside 'categories' array.
You should first use $unwind which outputs one document for each element in the array.
db.books.aggregate([
{
$unwind : "$categories"
},
{
$group : { _id : "$categories", total: { $sum: 1 } }
}
])

MongoDb query to find specific object in nested array

i have an array of object player_of_day inwhich there is again array of user-Ids i.e. nominate_by I want to find an object having maximum count.
Something like-
an object from player_of_day whose count is maximum
"player_of_day" : [
{
"nominate_by" : [
ObjectId("58994bca4187b412aec50c94"),
ObjectId("58994a124187b412aec50c91"),
ObjectId("589955d54187b412aec50c99")
],
"nominate_to" : ObjectId("58994b374187b412aec50c93"),
"count" : 3
},
{
"nominate_by" : [
ObjectId("58994c254187b412aec50c95"),
ObjectId("58994ad04187b412aec50c92")
],
"nominate_to" : ObjectId("58994a124187b412aec50c91"),
"count" : 2
},
{
"nominate_by" : [
ObjectId("58994b374187b412aec50c93")
],
"nominate_to" : ObjectId("58994c254187b412aec50c95"),
"count" : 1
}
],
you can use $unwind
db.match.aggregate([{"$unwind":"$player_of_day"},{"$sort":{'player_of_day.count':-1}},{"$limit":1}]);

MongoDB: Find all matched array element from single document

I have a mongodb document like this,
{
"_id" : ObjectId("4e8ae86d08101908e1000001"),
"eId": 101,
"about": "test",
"tags" : [
{"name": "jana"},
{"name":"bala"},
{"name":"jk"},
{"name":"charles"}
]
}
I need to find all matched array elements, where the name matched with given array.
db.coll.find({"tags": {"$elemMatch": {"name": {"$in": [/^jana/i, /^charles/i] }}}})
for this query i got the following result
{
"_id" : ObjectId("4e8ae86d08101908e1000001"),
"tags" : [
{
"name" : "jana"
}
]
}
$elemMatch query only return the first matched element, but i want all the matched array element like this,
{
"_id" : ObjectId("4e8ae86d08101908e1000001"),
"tags" : [
{
"name" : "jana"
},
{
"name" : "charles"
}
]
}
Is it possible to get the result like this?
note: i don't want any others fields, I want only the matched array elements along with _id
You can use MongoDB Aggregation Pipeline:
db.coll.aggregate([
{'$unwind': '$tags'},
{'$match':
{"tags.name":
{"$in": [/^jana/, /^charles/i] }
}
},
{'$group':
{
'_id': '$_id',
'tags':
{'$push': '$tags'}
}
}
])
Result : -
{
"result" : [
{
"_id" : ObjectId("5538b214706a90c718f75a41"),
"tags" : [
{
"name" : "jana"
},
{
"name" : "charles"
}
]
}
],
"ok" : 1
}

Searching JSON document in monogDB for value across array elements

I have some complex document (being new to mongodb schemas, I think it's complext) that I'm trying to process through for a specific array value match across different array sections of the document.
Sample content of my document:
{
"_id" : ObjectId("541c0c9bdfecb53368e12ef0"),
"SRVIP" : "10.10.10.10",
"INSNME" : "myinstance",
"DBNAME" : "mydbname",
"DBGRPL" : [{
"GRPNME" : "grp1",
"GRPPRV" : "7",
"GRPAUT" : [ “AUTH1”,”AUTH2”],
"GRPUSR" : [ "USER1",”USER2”]
}
],
"SAUTLV" : [ { "SAUNME" : "USER4",
"SAUPRV" : "0",
"SAUAUT" : [ “AUTH2”,”AUTH3”],
"SAUUSR" : [ "USER2" ]
}
],
"USRLVL" : [
{ "ULVNME" : "USER1",
"ULVPRV" : "0",
"ULVAUT" : [ "AUTH1","AUTH2","AUTH3"]
},
{
"ULVNME" : "USER2",
"ULVPRV" : "2411",
"ULVAUT" : [ "AUTH3"]
}
]
}
I'm trying to only return the section of the document where for example USER1 exists
At the moment, I've create two different aggregated statement to retrieve the information, but I'm looking at a single statement to search all arrays in the document.
Retrieving USER1 statement on DBGRPL array level :
var var1=[“USER1”]
db.authinfo.aggregate({$unwind:"$DBGRPL"},{$match:{"DBGRPL.GRPUSR":{$in:var1}}},{$project:{SRVIP:1,DBNAME:1,"DBGRPL":1}})
var var1=”USER1”
Retrieving USER1 statement on USRLVL array level:
db.authinfo.aggregate({$unwind:"$USRLVL"},{$match:{"USRLVL.ULVNME":var1}},{$project:{SRVIP:1,DBNAME:1,"USRLVL":1}})
The obvious error with the above approach is using 2 different variable type for the queries to work, which is also something I can't resolve at the moment ….
How can I combine the search into a single statement ?
expected output :
{
"_id" : ObjectId("541c0c9bdfecb53368e12ef0"),
"SRVIP" : "10.10.10.10",
"INSNME" : "myinstance",
"DBNAME" : "mydbname",
"DBGRPL" : [{
"GRPNME" : "grp1",
"GRPPRV" : "7",
"GRPAUT" : [ “AUTH1”,”AUTH2”],
"GRPUSR" : [ "USER1",”USER2”]
}
],
"USRLVL" : [
{ "ULVNME" : "USER1",
"ULVPRV" : "0",
"ULVAUT" : [ "AUTH1","AUTH2","AUTH3"]
}
{
]
}
when searching for USER1.
I will also search across the GRPAUTH, SAUAUT and ULVAUTH sections of the document where say AUTH1 is a value ...

Resources