Query inner value in mongoDB - mongodb

all
I'm trying to do a join in MongoDB but also, I need to check for conditions and to do a sum on inner values of what comes back from the join.
I will explain.
Currently I have this simple join query which looks like this:
db.Sets.aggregate([
{
$lookup:
{
from: "ExecutionTasks",
localField: "identifier",
foreignField: "setIdentifier",
as: "execTask"
}
}
])
It returns the following results:
/* 1 */
{
"_id" : 1,
"name" : "Demo Set",
"identifier" : "demo-set",
"description" : "Demo Set",
"creator" : {
"id" : 1,
"name" : "admin"
},
"createdDate" : ISODate("2017-03-24T20:09:55.120Z"),
"updatedDate" : ISODate("2017-03-24T20:09:55.120Z"),
"execTask" : [
{
"_id" : 1,
"isActive" : 1,
"type" : "count",
"threshold" : {
"default" : "0",
"deviations" : []
},
"name" : "amishay",
"setIdentifier" : "demo-set",
"description" : "a",
"query" : {
"source" : 1,
"text" : "select * from t"
},
"creator" : {
"id" : 1,
"name" : "admin"
},
"createdDate" : ISODate("2017-03-27T20:03:22.275Z"),
"updatedDate" : ISODate("2017-03-27T20:03:22.275Z")
},
{
"_id" : 2,
"isActive" : 0,
"type" : "count",
"threshold" : {
"default" : "0",
"deviations" : []
},
"name" : "amishay2",
"setIdentifier" : "demo-set",
"description" : "test",
"query" : {
"source" : 1,
"text" : "select * from t"
},
"creator" : {
"id" : 1,
"name" : "admin"
},
"createdDate" : ISODate("2017-03-27T20:03:57.248Z"),
"updatedDate" : ISODate("2017-03-27T20:03:57.248Z")
}
]
}
What I would like to do is to return only the length of the array (execTask) and also only those with the attribute isActive which equals to 1.
So basically I want to get something like:
{
"_id" : 1,
"name" : "Demo Set",
"identifier" : "demo-set",
"description" : "Demo Set",
"creator" : {
"id" : 1,
"name" : "admin"
},
"createdDate" : ISODate("2017-03-24T20:09:55.120Z"),
"updatedDate" : ISODate("2017-03-24T20:09:55.120Z"),
"execTask" : 1
}
I checked online numerous questions but I only saw examples which query the collection attribute and not the joined collection attribute.
Thanks!

You can add $addFields stage after $lookup. The below stage will $filter and calculate the $size for query criteria.
$filter operator is to used to filter the execTask array contents in-place on the mentioned criteria.
Expressions $ and $$ to reference the fields / aggregation operators / aggregation stages and inner variables respectively.
$size operator to calculate the length of filtered array.
$addFields overwrites the existing field execTask to replace its value with the calculated size.
{
$addFields: {
"execTask": {
$size: {
$filter: {
input: "$execTask",
as: "result",
cond: {
$eq: ["$$result.isActive", 1]
}
}
}
}
}
}

Related

Mongodb subdocument fields union

I need to join two collection from mongodb. First of all I have an aggregation like this:
db.messages.aggregate([
{
$lookup: {
from :"channels",
localField: "channels",
foreignField: "_id",
as: "merged_channels"
}
}
]).pretty()
after this aggragation my documents looks like this:
{
"_id" : "21ca6117-1f14-4613-9407-db7f3a011142",
"author" : "03072fad-a8fd-53f3-b25f-abbfaf15b055",
"title" : "test2",
"body" : "test2",
"channels" : [
"8008d5a8-eb3b-4e55-98c5-a60fd7275bd2",
"8008d5a8-eb3b-4e55-98c5-a60fd7275bd3"
],
"comments" : [
{
"author" : "03072fad-a8fd-53f3-b25f-abbfaf15b055",
"body" : "comment1",
"created_at" : ISODate("2018-03-15T07:08:10.018Z")
},
{
"author" : "03072fad-a8fd-53f3-b25f-abbfaf15b055",
"body" : "testbody",
"created_at" : ISODate("2018-03-15T07:08:09.366Z")
}
],
"created_at" : ISODate("2018-03-15T07:08:09.018Z"),
"updated_at" : ISODate("2018-03-15T07:08:09.366Z"),
"deleted_at" : null,
"merged_channels" : [
{
"_id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275bd2",
"author" : "03072fad-a8fd-53f3-b25f-abbfaf15b055",
"name" : "chan2",
"members" : [
{
"id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275cb5",
"type" : "group"
}
],
"created_at" : ISODate("2018-03-15T07:08:08.872Z"),
"updated_at" : ISODate("2018-03-15T07:08:08.872Z"),
"deleted_at" : null
},
{
"_id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275bd3",
"author" : "8008d5a8-eb3b-4e55-98c5-a60fd7275fg4",
"name" : "chan3",
"members" : [
{
"id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275cb5",
"type" : "group"
},
{
"id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275cb6",
"type" : "user"
},
{
"id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275cb7",
"type" : "role"
}
],
"created_at" : ISODate("2018-03-15T07:08:08.872Z"),
"updated_at" : ISODate("2018-03-15T07:08:09.358Z"),
"deleted_at" : null
}
]
}
I want to take members fields from chan2 and chan3, union them and put into the root (message) document. And then remove merged_channels field. Remove the merged field is not a problem, but have no idea how to extract fields from subobject and merge them.
How would I achieve this?
You can use below aggregation in 3.4.
$reduce to $concatArrays and project with exclusion to drop the merged_channels field.
db.col.aggregate({
"$addFields":{
"merged_arrays":{
"$reduce":{
"input":"$merged_channels",
"initialValue":[],
"in":{"$concatArrays":["$$value", "$$this.members"]}
}
}
},
{"$project":{"merged_channels":0}}
})

Getting array of object with limit and offset doesn't work using mongodb

First let me say that I am new to mongodb. I am trying to get the data from the collection
Here is the document in my collection student:
{
"_id" : ObjectId("5979e0473f00003717a9bd62"),
"id" : "l_7c0e37b9-132e-4054-adbf-649dbc29f43d",
"name" : "Raj",
"class" : "10th",
"assignments" : [
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc571",
"name" : "1"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc572",
"name" : "2"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc573",
"name" : "3"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc574",
"name" : "4"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc575",
"name" : "5"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc576",
"name" : "6"
}
]
}
the output which i require is
{
"assignments" : [
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc571",
"name" : "1"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc572",
"name" : "2"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc573",
"name" : "3"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc574",
"name" : "4"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc575",
"name" : "5"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc576",
"name" : "6"
}
]
}
for this response i used the following query
db.getCollection('student').find({},{"assignments":1})
Now what exactly I am trying is to apply limit and offset for the comments list I tried with $slice:[0,3] but it gives me whole document with sliced result
but not assignments alone so how can I combine these two in order to get only assignments with limit and offset.
You'll need to aggregate rather than find because aggregate allows you to project+slice.
Given the document from your question, the following command ...
db.getCollection('student').aggregate([
// project on assignments and apply a slice to the projection
{$project: {assignments: {$slice: ['$assignments', 2, 5]}}}
])
... returns:
{
"_id" : ObjectId("5979e0473f00003717a9bd62"),
"assignments" : [
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc573",
"name" : "3"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc574",
"name" : "4"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc575",
"name" : "5"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc576",
"name" : "6"
}
]
}
This represents the assignments array (and only the assignments array) with a slice from element 2 to 5. You can change the slice arguments (2, 5 in the above example) to apply your own offset and limit (where the first argument is the offset and the limit is the difference between the first and second arguments).
If you want to add a match condition (to address specific documents) to the above then you'd do something like this:
db.getCollection('other').aggregate([
/// match a specific document
{$match: {"_id": ObjectId("5979e0473f00003717a9bd62")}},
// project on assignments and apply a slice to the projection
{$project: {assignments: {$slice: ['$assignments', 2, 5]}}}
])
More details on the match step here.

Mongodb aggregate match array item with child array item

I would like to find documents that contains specific values in a child array.
This is an example document:
{
"_id" : ObjectId("52e9658e2a13df5be22cf7dc"),
"desc" : "Something somethingson",
"imageurl" : "http://",
"tags" : [
{
"y" : 29.3,
"brand" : "52d2cecd0bd1bd844d000018",
"brandname" : "Zara",
"type" : "Bow Tie",
"x" : 20,
"color" : "52d50c19f8f8ca8448000001",
"number" : 0,
"season" : 0,
"cloth" : "52d50d57f8f8ca8448000006"
},
{
"y" : 29.3,
"brand" : "52d2cecd0bd1bd844d000018",
"brandname" : "Zara",
"type" : "Bow Tie",
"x" : 20,
"color" : "52d50c19f8f8ca8448000001",
"number" : 0,
"season" : 0,
"cloth" : "52d50d57f8f8ca8448000006"
}
],
"user_id" : "52e953942a13df5be22cf7af",
"username" : "Thompson",
"created" : 1386710259971,
"occasion" : "ID",
"sex" : 0
}
The query I would like to do should look something like this:
db.posts.aggregate([
{$match: {tags.color:"52d50c19f8f8ca8448000001", tags.brand:"52d2cecd0bd1bd844d000018", occasion: "ID"}},
{$sort:{"created":-1}},
{$skip:0},
{$limit:10}
])
my problem is that I dont know how to match anything inside an array in the document like "tags". How can I do this?
You could try to do it without aggregation framework:
db.posts.find(
{
occasion: "ID",
tags: { $elemMatch: { color:"52d50c19f8f8ca8448000001", brand:"52d2cecd0bd1bd844d000018" } }
}
).sort({created: -1}).limit(10)
And if you want to use aggregation:
db.posts.aggregate([
{$match:
{
tags: { $elemMatch: { color:"52d50c19f8f8ca8448000001", brand: "52d2cecd0bd1bd844d000018" } },
occasion: "ID"
}
},
{$sort:{"created":-1}},
{$limit:10}
])

How to get exact document result from key value type of embedded documents

Let say I have this kind of document structured, the attributes field will be the embedded document
and I've already indexed the attributes.key and attributes.value
1-------------------------------------------------------------------------------------
{
"_id" : ObjectId( "5191d8e5d00560402e000001" ),
"attributes" : [
{ "key" : "pobox","value" : "QaKUWo" },
{ "key" : "city", "value" : "CBDRip" },
{ "key" : "address","value" : "zmycAa" } ],
"email" : "FWAUdl_2#email.com",
"firstname" : "FWAUdl_2"
}
2-------------------------------------------------------------------------------------
{
"_id" : ObjectId( "5191d8e7d00560402e000055" ),
"attributes" : [
{ "key" : "pobox", "value" : "sNFriy" },
{ "key" : "city", "value" : "JPdVrI" },
{ "key" : "address", "value" : "phOluW" } ],
"email" : "hqYNWH_86#email.com",
"firstname" : "hqYNWH_86"
}
My problem is how to get exact document when querying based only on the attributes field,
db.app.find({ attributes.key:address , attributes.value:/.*uw.*/i })
The query result is not as I expected, it should result only the 2nd document only without the 1st document.
I know that I put regex on the attributes.value, I was expecting that it only check for attributes.key that have address value.
And what if I want to filter another key, such like,
db.app.find({ attributes.key:address , attributes.value:/.*uw.*/i , attributes.key:city , attributes.value:/.*ri.*/i })
Any opinion will be helpful guys.
Thx.
I guess you need $elemMatch ( http://docs.mongodb.org/manual/reference/operator/elemMatch/ )
db.test123.find({ attributes : { $elemMatch : { 'key':"address" , 'value':/.*uw.*/i } } }).pretty()
{
"_id" : ObjectId("5191d8e7d00560402e000055"),
"attributes" : [
{
"key" : "pobox",
"value" : "sNFriy"
},
{
"key" : "city",
"value" : "JPdVrI"
},
{
"key" : "address",
"value" : "phOluW"
}
],
"email" : "hqYNWH_86#email.com",
"firstname" : "hqYNWH_86"
}
Just investigated a little and figured out following. The following uses the index mentioned below. You can do a explain() on the find() to check more index usage details
db.testing.getIndexKeys()
[ { "_id" : 1 }, { "attributes.key" : 1, "attributes.value" : 1 } ]
test:Mongo > db.testing.find({$and : [ { attributes : {$elemMatch : {key : 'address', value : /.*uw.*/i }} }, { attributes : {$elemMatch : {key : 'city', value : /.*ri.*/i }} }] }).pretty()
{
"_id" : ObjectId("5191d8e7d00560402e000055"),
"attributes" : [
{
"key" : "pobox",
"value" : "sNFriy"
},
{
"key" : "city",
"value" : "JPdVrI"
},
{
"key" : "address",
"value" : "phOluW"
}
],
"email" : "hqYNWH_86#email.com",
"firstname" : "hqYNWH_86"
}

MongoDB Aggregation Framework: Getting $unwind error when using $group

I have a document structure as follows:
{
"_id" : NumberLong("80000000012"),
[...]
"categories" : [{
"parent" : "MANUFACTURER",
"category" : "Chevrolet"
}, {
"parent" : "MISCELLANEOUS",
"category" : "Miscellaneous"
}],
[...]
}
I am trying to get a distinct list of all 'category' fields for each 'parent' field. I was trying to utilize the aggregation framework to do this with the following query:
db.posts_temp.aggregate(
{$unwind : '$categories'},
{$match : {'categories.parent' : 'MISCELLANEOUS'}},
{$project : {
'_id' : 0,
parent : '$categories.parent',
category : '$categories.category'
}
},
{
$group : {
_id : '$parent',
category : {$addToSet : '$category'}
}
}
);
Running this query returns the following error:
{
"errmsg" : "exception: $unwind: value at end of field path must be an array",
"code" : 15978,
"ok" : 0
}
This seems to be tied to the group portion of the query, because, when I remove it, the query runs correctly, but, obviously, the data is not where I want it to be.
I just tried executing the above aggregation query on my mongo instance. Here are my 3 documents each with a key of categories that has an array of two nested documents.
Here is my data:
{
"_id" : ObjectId("512d5252b748191fefbd4698"),
"categories" : [
{
"parent" : "MANUFACTURER",
"category" : "Chevrolet"
},
{
"parent" : "MISCELLANEOUS",
"category" : "Miscellaneous"
}
]
}
{
"_id" : ObjectId("512d535cb748191fefbd4699"),
"categories" : [
{
"parent" : "MANUFACTURER",
"category" : "Chevrolet"
},
{
"parent" : "MISCELLANEOUS",
"category" : "Pickup"
}
]
}
{
"_id" : ObjectId("512d536eb748191fefbd469a"),
"categories" : [
{
"parent" : "MANUFACTURER",
"category" : "Toyota"
},
{
"parent" : "MISCELLANEOUS",
"category" : "Miscellaneous"
}
]
}
Here is the aggregation query of yours that I ran:
db.posts_temp.aggregate( {$unwind:'$categories'} , {$match: {'categories.parent':'MISCELLANEOUS'}}, {$project:{'_id':0, parent: '$categories.parent', category:'$categories.category'}}, {$group:{_id:'$parent', category:{$addToSet:'$category'}}})
Here is the result:
{
"result" : [
{
"_id" : "MISCELLANEOUS",
"category" : [
"Pickup",
"Miscellaneous"
]
}
],
"ok" : 1
}
Let me know if there some discrepancies between my data and yours.
CSharpie