Apply $elemMatch to multiple arrays - mongodb

The result of my query is:
{
"_id" : ObjectId("5b5e680bca55f2885cb3c864"),
"barcelona" : [
{
"date" : ISODate("2018-07-01T00:00:00.000+0000"),
"name" : "david",
"age" : 17.0
}
]
}
My query looks like this:
db.test8.find({ line: '2' },{ barcelona: { $elemMatch: {
date :
{
"$gte" : ISODate("2018-07-01T00:00:00Z"),
"$lt" : ISODate("2018-07-02T00:00:00Z")
}
}}});
The dataset looks like this in the mongodb. It has three arrays: barcelona, paris, and london. I will answer any questions you may have.
{
"_id" : ObjectId("5b5e680bca55f2885cb3c864"),
"piwikID" : "2",
"barcelona" : [
{
"date" : ISODate("2018-07-01T00:00:00.000+0000"),
"name" : "david",
"age" : 11.0
},
{
"date" : ISODate("2018-07-02T00:00:00.000+0000"),
"name" : "damo",
"age" : 16.0
},
{
"date" : ISODate("2018-07-03T00:00:00.000+0000"),
"name" : "dani",
"age" : 12.0
}
],
"paris" : [
{
"date" : ISODate("2018-07-01T00:00:00.000+0000"),
"name" : "david",
"age" : 17.0
},
{
"date" : ISODate("2018-07-02T00:00:00.000+0000"),
"name" : "damo",
"age" : 10.0
},
{
"date" : ISODate("2018-07-03T00:00:00.000+0000"),
"name" : "danp",
"age" : 13.0
}
],
"london" : [
{
"date" : ISODate("2018-07-01T00:00:00.000+0000"),
"name" : "dan",
"age" : 11.0
},
{
"date" : ISODate("2018-07-02T00:00:00.000+0000"),
"name" : "donner",
"age" : 12.0
},
{
"date" : ISODate("2018-07-03T00:00:00.000+0000"),
"name" : "dangus",
"age" : 14.0
}
]
};
There are other cities paris, london, etc. which are arrays and should display in my result.
How can I modify my query to show them in my result as well?

You want to use $elemMatch for multiple arrays first, you need to build the dynamic query.
Suppose you know the city names before you queried the DB you can build a query like this.
var cities = ['barcelona', 'paris', 'london'];
var query = {
"$or": []
};
for (let i = 0; i < cities.length; i++) {
let queryObj = {};
queryObj[cities[i]] = {
"$elemMatch": {
date:
{
"$gte": ISODate("2018-07-01T00:00:00Z"),
"$lt": ISODate("2018-07-02T00:00:00Z")
}
}
};
query["$or"].push(queryObj);
};
Your query like this :
{
"$or":
[{ "barcelona": { "$elemMatch": { "date": { "$gte": ISODate("2018-07-01T00:00:00Z"), "$lt": ISODate("2018-07-02T00:00:00Z") } } } },
{ "paris": { "$elemMatch": { "date": { "$gte": ISODate("2018-07-01T00:00:00Z"), "$lt": ISODate("2018-07-02T00:00:00Z") } } } },
{ "london": { "$elemMatch": { "date": { "$gte": ISODate("2018-07-01T00:00:00Z"), "$lt": ISODate("2018-07-02T00:00:00Z") } } } }]
}
db.col.find(query);

Related

match element in the array with aggregation

i have mongo db collection the follwing structure
{
{
"_id" : ObjectId("63e37afe7a3453d5014c011b"),
"schemaVersion" : NumberInt(1),
"Id" : "ObjectId("63e37afe7a3453d5014c0112")",
"Id1" : "ObjectId("63e37afe7a3453d5014c0113")",
"Id2" : "ObjectId("63e37afe7a3453d5014c0114")",
"collectionName" : "Country",
"List" : [
{
"countryId" : NumberInt(1),
"name" : "Afghanistan",
},{
"countryId" : NumberInt(1),
"name" : "India",
},
{
"countryId" : NumberInt(1),
"name" : "USA",
}
}
i need to match the value with id, id1, id2, collectionName and name in the list to get country id for example if match the below value
"Id" : "ObjectId("63e37afe7a3453d5014c0112")",
"Id1" : "ObjectId("63e37afe7a3453d5014c0113")",
"Id2" : "ObjectId("63e37afe7a3453d5014c0114")",
"collectionName" : "Country",
"name" : "Afghanistan",
i need result
{
"countryId" : 1,
"name" : "Afghanistan",
}
i tried like below
db.country_admin.aggregate([
{ $match: { collectionName: "Country" } },
{ $unwind : '$countryList' },
{ $project : { _id : 0, 'countryList.name' : 1, 'countryList.countryId' : 1 } }
]).pretty()
and i have following output
[
{
"List" : {
"countryId" : 1.0,
"name" : "Afghanistan"
}
},
{
"List" : {
"countryId" : 2.0,
"name" : "india"
}
},
{
"List" : {
"countryId" : 3.0,
"name" : "USA"
}
}]```
You can try using $filter to avoid $unwind like this example:
First $match by your desired condition(s).
Then $filter and get the first element (as "List.name": "Afghanistan" is used into $match stage there will be at least one result).
And output only values you want using $project.
db.collection.aggregate([
{
"$match": {
"Id": ObjectId("63e37afe7a3453d5014c0112"),
"Id1": ObjectId("63e37afe7a3453d5014c0113"),
"Id2": ObjectId("63e37afe7a3453d5014c0114"),
"collectionName": "Country",
"List.name": "Afghanistan",
}
},
{
"$project": {
"country": {
"$arrayElemAt": [
{
"$filter": {
"input": "$List",
"cond": {
"$eq": [
"$$this.name",
"Afghanistan"
]
}
}
},
0
]
}
}
},
{
"$project": {
"_id": 0,
"countryId": "$country.countryId",
"name": "$country.name"
}
}
])
Example here
By the way, using $unwind is also possible and you can check this example

mongodb find the document by id and then group the result based on name field

I have a collection with multiple documents like
{
"_id" : ObjectId("5a64d076bfd103df081967ae"),
"status" : "",
"Number" : 53,
"values" : [
{
"date" : "2015-05-18",
"value" : 12.41
},
{
"date" : "2015-05-19",
"value" : 12.45
},
],
"Name" : "ABC Banking",
"scheme":"ABC1",
"createdDate" : "21-01-2018"
}
{
"_id" : ObjectId("5a64d076bfd103df081967ae"),
"status" : "",
"Number" : 53,
"values" : [
{
"date" : "2015-05-18",
"value" : 13.41
},
{
"date" : "2015-05-19",
"value" : 13.45
},
],
"Name" : "ABC Banking",
"scheme":"ABC2",
"createdDate" : "21-01-2018"
}
I am Querying collection based on Number field like
db.getCollection('mfhistories').find({'Number':53})
to get all the documents with this Number.
Now I want to group all the collection with Name 'ABC Banking' into an array. so that I will get result based on Name.
so the result should be like
{
"Name":"ABC Banking",
[
{
"_id" : ObjectId("5a64d076bfd103df081967ae"),
"status" : "",
"Number" : 53,
"values" : [
{
"date" : "2015-05-18",
"value" : 13.41
},
{
"date" : "2015-05-19",
"value" : 13.45
},
],
"scheme":"ABC1",
"createdDate" : "21-01-2018"
},
{
"_id" : ObjectId("5a64d076bfd103df081967ae"),
"status" : "",
"Number" : 53,
"values" : [
{
"date" : "2015-05-18",
"value" : 13.41
},
{
"date" : "2015-05-19",
"value" : 13.45
}
],
"scheme":"ABC2",
"createdDate" : "21-01-2018"
}
]
}
Please help..
Thanks,
J
You can use Aggregation Framework for that:
db.col.aggregate([
{
$match: { Number: 53, Name: "ABC Banking" }
},
{
$group: {
_id: "$Name",
docs: { $push: "$$ROOT" }
}
},
{
$project: {
Name: "$_id",
_id: 0,
docs: 1
}
}
])
$$ROOT is a special variable which captures entire document. More here.
db.mfhistories.aggregate(
// Pipeline
[
// Stage 1
{
$match: {
Number: 53
}
},
// Stage 2
{
$group: {
_id: {
Name: '$Name'
},
docObj: {
$addToSet: '$$CURRENT'
}
}
},
// Stage 3
{
$project: {
Name: '$_id.Name',
docObj: 1,
_id: 0
}
}
]
);

Aggregate using $group twice

I've read SO and questions like this one. However I'm not able to build the query I want...
Let's say I have the following data structure:
{
"CAUG" : "id1",
"action" : "actionA",
"date" : ISODate("2017-01-01"),
"hp" : 16
}
{
"CAUG" : "id1",
"action" : "actionB",
"date" : ISODate("2017-01-01"),
"hp" : 17
}
{
"CAUG" : "id1",
"action" : "actionC",
"date" : ISODate("2017-02-10"),
"hp" : 18
}
{
"CAUG" : "id2",
"action" : "actionX",
"date" : ISODate("2018-01-01"),
"hp" : 20
}...
The desired output is something like (not sure about brackets and other stuff...):
{
"CAUG" : "id1",
"timeline" : [
ISODate ("2017-01-01) {
{ "action" : "ActionA", hp : "..." }
{ "action" : "ActionB", hp : "..." }
},
ISODate ("2017-02-10) {
{ "action" : "ActionC", hp : "..." }
}
]
}
{
"CAUG" : "id2",
"timeline" : [
ISODate ("2018-01-01) {
{ "action" : "ActionX", hp : "..." }
}
]
}
At this time my (very limited) query is:
(I've tried many things like composite _id, but I'm alway stucked at some point).
db.aggregate(
[
{ $match: { something } },
{ $project: { something } },
{ $group: {
_id: '$CAUG',
"timeline": { "$push": "$$ROOT" }
}
}
]
)
The problem is I do not know how to do another $group inside timeline array... I'm stucked with the output below... Any clue please? Have a nice weekend.
{
"_id" : "1",
"timeline" : [
{
"CAUG" : "ca220491-ug43816",
"action" : "actionA",
"date" : ISODate("2016-12-21T23:00:00.000+0000")
},
{
"CAUG" : "ca220491-ug43816",
"action" : "actionB",
"date" : ISODate("2016-12-21T23:00:00.000+0000")
},
{
"CAUG" : "ca220491-ug43816",
"action" : "actionC",
"date" : ISODate("2017-02-21T23:00:00.000+0000")
}
]
}
Try running the following aggregate operation:
db.collection.aggregate([
{
"$group": {
"_id": {
"CAUG": "$CAUG",
"date": {
"$dateToString": {
"format": "%Y-%m-%d",
"date": "$date"
}
}
},
"docs": {
"$push": {
"action" : "$action",
"hp" : "$hp"
}
}
}
},
{
"$group": {
"_id": "$_id.CAUG",
"timeline": {
"$push": {
"date": "$_id.date",
"docs": "$docs"
}
}
}
}
])
which gives the sample output
/* 1 */
{
"_id" : "id1",
"timeline" : [
{
"date" : "2017-02-10",
"docs" : [
{
"action" : "actionC",
"hp" : 18.0
}
]
},
{
"date" : "2017-01-01",
"docs" : [
{
"action" : "actionA",
"hp" : 16.0
},
{
"action" : "actionB",
"hp" : 17.0
}
]
}
]
}
/* 2 */
{
"_id" : "id2",
"timeline" : [
{
"date" : "2018-01-01",
"docs" : [
{
"action" : "actionX",
"hp" : 20.0
}
]
}
]
}

mongodb aggregation $group and then $push a object

this is my data :
> db.bookmarks.find({"userId" : "56b9b74bf976ab70ff6b9999"}).pretty()
{
"_id" : ObjectId("56c2210fee4a33579f4202dd"),
"userId" : "56b9b74bf976ab70ff6b9999",
"items" : [
{
"itemId" : "28",
"timestamp" : "2016-02-12T18:07:28Z"
},
{
"itemId" : "29",
"timestamp" : "2016-02-12T18:07:29Z"
},
{
"itemId" : "30",
"timestamp" : "2016-02-12T18:07:30Z"
},
{
"itemId" : "31",
"timestamp" : "2016-02-12T18:07:31Z"
},
{
"itemId" : "32",
"timestamp" : "2016-02-12T18:07:32Z"
},
{
"itemId" : "33",
"timestamp" : "2016-02-12T18:07:33Z"
},
{
"itemId" : "34",
"timestamp" : "2016-02-12T18:07:34Z"
}
]
}
I want to have something like (actually i hope the _id can become userId too) :
{
"_id" : "56b9b74bf976ab70ff6b9999",
"items" : [
{ "itemId": "32", "timestamp": "2016-02-12T18:07:32Z" },
{ "itemId": "31", "timestamp": "2016-02-12T18:07:31Z" },
{ "itemId": "30", "timestamp": "2016-02-12T18:07:30Z" }
]
}
What I have now :
> db.bookmarks.aggregate(
... { $match: { "userId" : "56b9b74bf976ab70ff6b9999" } },
... { $unwind: '$items' },
... { $sort: { 'items.timestamp': -1} },
... { $skip: 2 },
... { $limit: 3},
... { $group: { '_id': '$userId' , items: { $push: '$items.itemId' } } }
... ).pretty()
{ "_id" : "56b9b74bf976ab70ff6b9999", "items" : [ "32", "31", "30" ] }
i tried to read the document in mongo and find out i can $push, but somehow i cannot find a way to push such object, which is not defined anywhere in the whole object. I want to have the timestamp also.. but i don't know how should i modified the $group (or others??) to do so. thanks for helping!
This code, which I tested in the MongoDB 3.2.1 shell, should give you the output format that you want:
> db.bookmarks.aggregate(
{ "$match" : { "userId" : "Ursula" } },
{ "$unwind" : "$items" },
{ "$sort" : { "items.timestamp" : -1 } },
{ "$skip" : 2 },
{ "$limit" : 3 },
{ "$group" : { "_id" : "$userId", items: { "$push" : { "myPlace" : "$items.itemId", "myStamp" : "$items.timestamp" } } } } ).pretty()
Running the above will produce this output:
{
"_id" : "Ursula",
"items" : [
{
"myPlace" : "52",
"myStamp" : ISODate("2016-02-13T18:07:32Z")
},
{
"myPlace" : "51",
"myStamp" : ISODate("2016-02-13T18:07:31Z")
},
{
"myPlace" : "50",
"myStamp" : ISODate("2016-02-13T18:07:30Z")
}
]
}
In MongoDB version 3.2.x, you can also use the $out operator in the very last stage of the aggregation pipeline, and have the output of the aggregation query written to a collection. Here is the code I used:
> db.bookmarks.aggregate(
{ "$match" : { "userId" : "Ursula" } },
{ "$unwind" : "$items" },
{ "$sort" : { "items.timestamp" : -1 } },
{ "$skip" : 2 },
{ "$limit" : 3 },
{ "$group" : { "_id" : "$userId", items: { "$push" : { "myPlace" : "$items.itemId", "myStamp" : "$items.timestamp" } } } },
{ "$out" : "ursula" } )
This gives me a collection named "ursula":
> show collections
ursula
and I can query that collection:
> db.ursula.find().pretty()
{
"_id" : "Ursula",
"items" : [
{
"myPlace" : "52",
"myStamp" : ISODate("2016-02-13T18:07:32Z")
},
{
"myPlace" : "51",
"myStamp" : ISODate("2016-02-13T18:07:31Z")
},
{
"myPlace" : "50",
"myStamp" : ISODate("2016-02-13T18:07:30Z")
}
]
}
>
Last of all, this is the input document I used in the aggregation query. You can compare this document to how I coded the aggregation query to see how I built the new items array.
> db.bookmarks.find( { "userId" : "Ursula" } ).pretty()
{
"_id" : ObjectId("56c240ed55f2f6004dc3b25c"),
"userId" : "Ursula",
"items" : [
{
"itemId" : "48",
"timestamp" : ISODate("2016-02-13T18:07:28Z")
},
{
"itemId" : "49",
"timestamp" : ISODate("2016-02-13T18:07:29Z")
},
{
"itemId" : "50",
"timestamp" : ISODate("2016-02-13T18:07:30Z")
},
{
"itemId" : "51",
"timestamp" : ISODate("2016-02-13T18:07:31Z")
},
{
"itemId" : "52",
"timestamp" : ISODate("2016-02-13T18:07:32Z")
},
{
"itemId" : "53",
"timestamp" : ISODate("2016-02-13T18:07:33Z")
},
{
"itemId" : "54",
"timestamp" : ISODate("2016-02-13T18:07:34Z")
}
]
}

mongodb aggregation match multiple $and on the same field

i have a document like this :
{
"ExtraFields" : [
{
"value" : "print",
"fieldID" : ObjectId("5535627631efa0843554b0ea")
},
{
"value" : "14",
"fieldID" : ObjectId("5535627631efa0843554b0eb")
},
{
"value" : "POLYE",
"fieldID" : ObjectId("5535627631efa0843554b0ec")
},
{
"value" : "30",
"fieldID" : ObjectId("5535627631efa0843554b0ed")
},
{
"value" : "0",
"fieldID" : ObjectId("5535627631efa0843554b0ee")
},
{
"value" : "0",
"fieldID" : ObjectId("5535627731efa0843554b0ef")
},
{
"value" : "0",
"fieldID" : ObjectId("5535627831efa0843554b0f0")
},
{
"value" : "42",
"fieldID" : ObjectId("5535627831efa0843554b0f1")
},
{
"value" : "30",
"fieldID" : ObjectId("5535627831efa0843554b0f2")
},
{
"value" : "14",
"fieldID" : ObjectId("5535627831efa0843554b0f3")
},
{
"value" : "19",
"fieldID" : ObjectId("5535627831efa0843554b0f4")
}
],
"id" : ObjectId("55369e60733e4914550832d0"), "title" : "A product"
}
what i want is to match one or more sets from the ExtraFields array. For example, all the products that contain the values print and 30. Since a value may be found in more than one fieldID (like 0 or true) we need to create a set like
WHERE (fieldID : ObjectId("5535627631efa0843554b0ea"), value : "print")
Where i'm having problems is when querying more than one fields. The pipeline i came up with is :
db.products.aggregate([
{'$unwind': '$ExtraFields'},
{
'$match': {
'$and': [{
'$and': [{'ExtraFields.value': {'$in': ["A52A2A"]}}, {
'ExtraFields.fieldID': ObjectId("5535627631efa0843554b0ea")
}]
}
,
{
'$and': [{'ExtraFields.value': '14'}, {'ExtraFields.fieldID': ObjectId("5535627631efa0843554b0eb")}]
}
]
}
},
]);
This returns zero results, but this is what i want to do in theory. Match all items that contain set 1 AND all that contain set 2.
The end result should look like a faceted search output :
[
{
"_id" : {
"values" : "18",
"fieldID" : ObjectId("5535627831efa0843554b0f3")
},
"count" : 2
},
{
"_id" : {
"values" : "33",
"fieldID" : ObjectId("5535627831efa0843554b0f2")
},
"count" : 1
}
]
Any ideas?
You could try the following aggregation pipeline
db.products.aggregate([
{
"$match": {
"ExtraFields.value": { "$in": ["A52A2A", "14"] },
"ExtraFields.fieldID": {
"$in": [
ObjectId("5535627631efa0843554b0ea"),
ObjectId("5535627631efa0843554b0eb")
]
}
}
},
{
"$unwind": "$ExtraFields"
},
{
"$match": {
"ExtraFields.value": { "$in": ["A52A2A", "14"] },
"ExtraFields.fieldID": {
"$in": [
ObjectId("5535627631efa0843554b0ea"),
ObjectId("5535627631efa0843554b0eb")
]
}
}
},
{
"$group": {
"_id": {
"value": "$ExtraFields.value",
"fieldID": "$ExtraFields.fieldID"
},
"count": {
"$sum": 1
}
}
}
])
With the sample document provided, this gives the output:
/* 1 */
{
"result" : [
{
"_id" : {
"value" : "14",
"fieldID" : ObjectId("5535627631efa0843554b0eb")
},
"count" : 1
}
],
"ok" : 1
}