I'm having multiple documents in a collection, each document has this data structure :
{
"_id": {
"$id": "5429409c9ac25ebe338b4567"
},
"data": [
{
"data_id": "70",
"info_data": [
{
"data_id": "98",
"data_index": 0,
"value": "info data"
},
{
"data_id": "99",
"data_index": 0,
"value": "some info data
}
]
},
{
"data_id": "71",
"info_data": [
{
"data_id": "98",
"data_index": 0,
"value": "some data"
},
{
"data_id": "99",
"data_index": 0,
"value": "more data"
}
]
}
]
},
{
"_id": {
"$id": "542940ac9ac25ef6358b4567"
},
"data": [
{
....
I need to conditionally sort these documents. for example I need to sort all the data.info_data documents only when the data.info_data.data_id = 98 by data.info_data.value only
So basically I need to sort an inner document that only matches to some criteria (the inner document no the external one).
I guess I need to use aggregation with unwind but I'm not sure how.
Related
I have the following mongodb documents:
{
"_id": "",
"name": "example1",
"colors": [
{
"id": 1000000,
"properties": [
{
"id": "1000",
"name": "",
"value": "green"
},
{
"id": "2000",
"name": "",
"value": "circle"
}
]
} ]
}
{
"_id": "",
"name": "example2",
"colors": [
{
"id": 1000000,
"properties": [
{
"id": "1000",
"name": "",
"value": "red"
},
{
"id": "4000",
"name": "",
"value": "box"
}
]
} ]
}
I would like to get distinct queries on the value field in the array where id=1000
db.getCollection('product').distinct('colors.properties.value', {'colors.properties.id':{'$eq': 1000}})
but it returns all values in the array.
The expected Result would be:
["green", "red"]
There are a lot of way to do.
$match eliminates unwanted data
$unwind de-structure the array
$addToSet in $group gives the distinct data
The mongo script :
db.collection.aggregate([
{
$match: {
"colors.properties.id": "1000"
}
},
{
"$unwind": "$colors"
},
{
"$unwind": "$colors.properties"
},
{
$match: {
"colors.properties.id": "1000"
}
},
{
$group: {
_id: null,
distinctData: {
$addToSet: "$colors.properties.value"
}
}
}
])
Working Mongo playground
I have a lot of complex JSON objects which are placed in the collection. For example:
{
"name": "Mike",
"price": "444",
"distance": 881,
"someFiend": 123,
"lots": [
{
"aa": "111",
"bb": "222"
},
{
"xx": "000"
}
],
"apps": [
{
"app": 1
},
{
"app": 2
}
]
}
I only want to project only those fields which are present in the following reference document:
{
"name": "",
"price": "",
"lots": [
{
"aa": "",
"bb": ""
}
]
}
Expected output:
{
"name": "Mike",
"price": "444",
"lots": [
{
"aa": "111",
"bb": "222"
}
]
}
Is there any way to iterate all documents in the collection and then filter out fields that are not present in the reference doc?
I want to aggregate data for the following sample array.
[
{
"_id": "5b7c0540342100091a375793",
"pages": [
{
"name": "ABCD",
"sections": [
{
"name": "sectionThird",
"id": 2,
"value": [
10,
50,
20
]
}
]
}
]
},
{
"_id": "5b3cd546342100514b4683a2",
"pages": [
{
"name": "ABCD",
"sections": [
{
"name": "sectionFourth",
"id": 2,
"value": [
19,
5,
8
]
},
{
"name": "sectionThird",
"id": 2,
"value": [
60
]
}
]
},
{
"name": "EFGH",
"sections": [
{
"name": "sectionFourth",
"id": 2,
"value": [
5
]
},
{
"name": "sectionThsads",
"id": 2,
"value": [
8
]
}
]
}
]
}
]
I want the following output:
[
{
"page": "ABCD",
"sections": [
{
"name": "sectionThird",
"totalValue": 140
},
{
"name": "sectionFourth",
"totalValue": 32
}
]
},
{
"page": "EFGH",
"sections": [
{
"name": "sectionFourth",
"totalValue": 5
},
{
"name": "sectionThsads",
"totalValue": 8
}
]
}
]
In the above sample array, you can see there are multiple documents with "page" as one of the keys which are also an array of objects. Each page object has a key "name" which is going to be unique for each object in "page" array. The "page" object has "sections" key and they also have "name" key in them which is going to be unique for each object.
So the output array is grouped by page.name then in that its grouped by sections.name from all the page objects with the sum of all the value array throughout sections inside a page object with the same section name.
You can use below aggregation.
$unwind each page and section followed by $group with $sum to sum the values for each section and $push to push the sections values back into page array.
db.col.aggregate([
{"$unwind":"$pages"},
{"$unwind":"$pages.sections"},
{"$group":{
"_id":{"pagename":"$pages.name","sectionname":"$pages.sections.name"},
"totalTime":{"$sum":{"$sum":"$pages.sections.value"}}
}},
{"$group":{
"_id":"$_id.pagename",
"sections":{"$push":{"name":"$_id.sectionname","totalTime":"$totalTime"}}
}}])
In Mongo 3.4
I have a collection with documents in the format:
Type1:
{
"Level1": {
"#version": "genR",
"#revision": "aux",
"Level2": {
"container": {
"type": "ARRAY",
"categories": [
{
"category": [
{
"Type": "STRING",
"Value": "Currency"
},
{
"Type": "STRING",
"Value": "EUR"
}
]
},
{
"category": [
{
"Type": "STRING",
"Value": "Portfolio"
},
{
"Type": "STRING",
"Value": "ABCDEF"
}
]
},
]
}
}
}
}
Type 2:
{
"Level1": {
"#version": "genR",
"#revision": "aux",
"Level2": {
"container": {
"type": "ARRAY",
"categories": [
{
"category": [
{
"Type": "STRING",
"Value": "Currency"
},
{
"Type": "STRING",
"Value": "EUR"
}
]
},
{
"category": [
{
"Type": "STRING",
"Value": "Portfolio"
},
{
"Type": "STRING",
"Value": "ABCDEF"
}
]
},
{
"category": [
{
"Type": "STRING",
"Value": "Short Description"
},
{
"Type": "STRING",
"Value": "Cash Only"
}
]
},
]
}
}
}
}
How do i write an aggregate statement so that I get ALL the Currency Values, ONLY from the documents where Portfolio matches a certain value.
I have been using pymongo's aggregate framework as below:
pipeline = [{"$unwind":"$Level1.Level2.container.categories"},{"$unwind":"$Level1.Level2.container.categories.category"},{"$match":{"Level1.Level2.container.categories.category.Value":"Portfolio"}}]
pprint(db.command('aggregate',collection,pipeline=pipeline))
But no results. Pymongo is a little confusing. Even if someone can point the general approach, it would really help.
The expected response assuming 4 matching documents (each with varying number of category items) is:
{'Currency': [{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}},
{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}},
{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'USD'}}}}}},
{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}}]}
Your structure is not ideal but you can use below query.
The below $match stage $ands two conditions. Looks in category array ($elemMatch) under categories ($elemMatch) array for elements satisfying both ($all) Portfolio match with ABCDEF value condition followed by condition for element with Currrency value.
$unwind stage is to break down the categories followed by $match to keep the Currency category embedded array documents.
$unwind stage is to break down the category followed by $match to remove the Currency Value embedded document.
Final two stages is to $group + $push the remaining data into embedded array and $project the Currency value.
You can run one stage at a time to view the intermediate output for better understanding.
db.collection.aggregate(
{ $match :
{ $and :
[
{ "Level1.Level2.container.categories":
{ $elemMatch:
{ "category":
{ $all:
[
{ $elemMatch : { "Type": "STRING", "Value": "Portfolio" } },
{ $elemMatch : { "Type": "STRING", "Value": "ABCDEF" } }
]
}
}
}
},
{ "Level1.Level2.container.categories":
{ $elemMatch:
{ "category":
{ $elemMatch : { "Type": "STRING", "Value": "Currency" } }
}
}
}
]
}
},
{ $unwind : "$Level1.Level2.container.categories" },
{ $match : { "Level1.Level2.container.categories.category.Value": "Currency" } },
{ $unwind : "$Level1.Level2.container.categories.category" },
{ $match : { "Level1.Level2.container.categories.category.Value": { $ne : "Currency" } } },
{ $group: { _id: null, "Currency": { $push: "$$ROOT" } } },
{ $project: { _id: 0, "Currency.Level1.Level2.container.categories.category.Value": 1 } } )
I have a mongo DB with several collections that contain JSON document formats shown below:
{
"questions": [
{
"questionEntry": {
"id": 1,
"info": {
"seasonNumber": 1,
"episodeNumber": 1,
"episodeName": "Days Gone Bye"
},
"questionItem": {
"theQuestion": "q1",
"attachedElement": {
"type": 1,
"value": ""
}
},
"options": [
{
"type": 1,
"value": "o1"
},
{
"type": 1,
"value": "o1"
}
],
"answer": {
"questionId": 1,
"answer": 1
},
"metaTags": [
"Season 1",
"Episode 1",
"Rick Grimmes"
]
}
},
{
"questionEntry": {
"id": 1,
"info": {
"seasonNumber": 1,
"episodeNumber": 1,
"episodeName": "Days Gone Bye"
},
"questionItem": {
"theQuestion": "q2",
"attachedElement": {
"type": 1,
"value": ""
}
},
"options": [
{
"type": 1,
"value": "o2"
},
{
"type": 1,
"value": "o2"
}
],
"answer": {
"questionId": 1,
"answer": 1
},
"metaTags": [
"Season 1",
"Episode 1",
"Rick Grimmes",
"Glenn Rhee"
]
}
}
]
}
I'm able to search for questions.questionEntry.questionItem.theQuestion for a matching criteria with:
db.questions.find({"questions.questionEntry.questionItem.theQuestion" : "q1"},{'questions.$':1}).pretty()
This works well for the questions collection but how would I do the same search across multiple collections?
Many thanks
To use the same query across multiple collections you may have to use the JavaScript bracket notation to access the collections in a loop. For example, the following queries the records database for all the collections (using the db.getCollectionNames() command) with the specified query:
use records
var colls = db.getCollectionNames(), // get all the collections in records db
query = {"questions.questionEntry.questionItem.theQuestion" : "q1"},
projection = {"questions.$": 1};
colls.forEach(function (collection){
var docs = db[collection].find(query, projection).toArray(); // use the bracket notation
docs.forEach(function (doc){ printjson(doc); });
})
You will have to do this by yourself. There is no out-of-the-box support.
You can query MongoDB multi-threaded (depending on your programming language) and aggregate the results to a unified result.