Find and sort array of array in nested documents - mongodb

My collection schema is like this:
{
"_id" : ObjectId("5f0c64e4dd0a36b93c7deafa"),
"name" : "Asd",
"email" : "asd#asd.com",
"password" : "$2b$12$66OTK8mSWELMF5YiF9HMUuHEeOVLI61aINjWs1Cmn1699lLJfz/7y",
"auto_ml" : true,
"notification" : true,
"photo" : null,
"tariff_id" : NumberInt(1),
"city" : null,
"sub_district" : null,
"village" : null,
"latitude" : null,
"longitude" : null,
"created_at" : ISODate("2020-07-13T20:43:00.871+0000"),
"updated_at" : ISODate("2020-07-13T23:08:26.149+0000"),
"family_members" : [
],
"rooms" : [
{
"_id" : ObjectId("5f0c98826f0321f6986755da"),
"name" : "Ruang Makan",
"created_at" : ISODate("2020-07-14T00:23:14.839+0000"),
"updated_at" : ISODate("2020-07-14T00:23:14.840+0000"),
"devices" : [
]
},
{
"_id" : ObjectId("5f0c98876f0321f6986755dd"),
"name" : "Ruang Tamu",
"created_at" : ISODate("2020-07-14T00:23:19.693+0000"),
"updated_at" : ISODate("2020-07-14T19:00:08.281+0000"),
"devices" : [
{
"serial_number" : "ST9L0CY4A2AVY7HFWWUE",
"used_relay" : NumberInt(0),
"created_at" : ISODate("2020-07-14T16:56:22.156+0000"),
"updated_at" : ISODate("2020-07-14T16:56:22.156+0000"),
"sensors" : [
{
"_id" : ObjectId("5f0db478ca203bde99d2438e"),
"name" : "Temperature",
"value" : null,
"created_at" : ISODate("2020-07-14T20:34:48.134+0000"),
"updated_at" : ISODate("2020-07-14T20:34:48.134+0000")
},
{
"_id" : ObjectId("5f0dbe0563ccbcb2a2aecc04"),
"name" : "Motion",
"value" : null,
"created_at" : ISODate("2020-07-14T21:15:33.135+0000"),
"updated_at" : ISODate("2020-07-14T21:15:33.135+0000")
},
{
"_id" : ObjectId("5f0dc0412022e93af338316f"),
"name" : "Humidity",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:05.126+0000"),
"updated_at" : ISODate("2020-07-14T21:25:05.126+0000")
},
{
"_id" : ObjectId("5f0dc0442022e93af3383170"),
"name" : "Light",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:08.451+0000"),
"updated_at" : ISODate("2020-07-14T21:25:08.451+0000")
}
],
"switches" : [
]
}
]
}
]
}
The question is, how could i access the array sensors inside device with "serial_number": "ST9L0CY4A2AVY7HFWWUE" and inside room with id ObjectId("5f0c98876f0321f6986755dd"), and the sort the sensors by id? I've tried the projection way and aggregate way but the result is not as i expected. My expected result is only showing the array of sensors like:
"sensors" : [
{
"_id" : ObjectId("5f0db478ca203bde99d2438e"),
"name" : "Temperature",
"value" : null,
"created_at" : ISODate("2020-07-14T20:34:48.134+0000"),
"updated_at" : ISODate("2020-07-14T20:34:48.134+0000")
},
{
"_id" : ObjectId("5f0dbe0563ccbcb2a2aecc04"),
"name" : "Motion",
"value" : null,
"created_at" : ISODate("2020-07-14T21:15:33.135+0000"),
"updated_at" : ISODate("2020-07-14T21:15:33.135+0000")
},
{
"_id" : ObjectId("5f0dc0412022e93af338316f"),
"name" : "Humidity",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:05.126+0000"),
"updated_at" : ISODate("2020-07-14T21:25:05.126+0000")
},
{
"_id" : ObjectId("5f0dc0442022e93af3383170"),
"name" : "Light",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:08.451+0000"),
"updated_at" : ISODate("2020-07-14T21:25:08.451+0000")
}
],
Query that i have tried:
db.users.aggregate([
{
$match: { "_id": ObjectId("5f0c64e4dd0a36b93c7deafa") }
},
{ $unwind: "$rooms" },
{
$match: {
"rooms._id": ObjectId("5f0c98876f0321f6986755dd")
}
},
{ $unwind: "$rooms.devices" },
{
$match: {
"rooms.devices.serial_number": "ST9L0CY4A2AVY7HFWWUE"
}
},
{
$project: { "rooms.devices.sensors": 1 }
},
{
$group: {
_id: "$_id",
sensors: { $first: "$rooms.devices.sensors" }
}
},
{
$sort: { "rooms.devices.sensors._id": -1}
},
])
But the sensors doesn't seem to be sorted.
Thanks in advance for your help~

Related

Mongo: Query to get count of objects in a nested objects in array

I need to retrieve this data if I have items in funds,
or to retrieve fields with items count.
query :
db.getCollection('contact')
.aggregate([ {$match: {guid: “456”}},
{$unwind: '$accounts'},
{
$lookup: {
from: 'accounts',
localField: 'accounts.id',
foreignField: 'id',
as: 'accounts_info'
}
},
{$project: {'accounts_info.subAccounts.items': 1, 'contacts.Id': 1, fullName : 1}}
]) .toArray();
result:
[
{
"_id" : “234”,
"accounts" : [
{
“subAccounts : {
"items" : [
{
"isExitDataReady" : false,
"Name" : “aaaa”,
"exitDate" : null,
"date" : "2019-08-14T00:00:00+03:00",
"CreatedDate" : "2020-03-05T10:03:40.000+0000",
"Id" : “00000000”,
},
{
"isExitDataReady" : false,
"Name" : “aaaa”,
"exitDate" : null,
"date" : "2019-08-14T00:00:00+03:00",
"CreatedDate" : "2020-03-05T10:03:40.000+0000",
"Id" : “00000000”,
},
{
"isExitDataReady" : false,
"Name" : “aaaa”,
"exitDate" : null,
"date" : "2019-08-14T00:00:00+03:00",
"CreatedDate" : "2020-03-05T10:03:40.000+0000",
"Id" : “00000000”,
},
{
"isExitDataReady" : false,
"Name" : “aaaa”,
"exitDate" : null,
"date" : "2019-08-14T00:00:00+03:00",
"CreatedDate" : "2020-03-05T10:03:40.000+0000",
"Id" : “00000000”,
},
{
"isExitDataReady" : false,
"Name" : “aaaa”,
"exitDate" : null,
"date" : "2019-08-14T00:00:00+03:00",
"CreatedDate" : "2020-03-05T10:03:40.000+0000",
"Id" : “00000000”,
}
]
}
}
]
},
{
"_id" : “567,
"subAccounts" : [
{
“funsd” : {
"items" : [
{
"isExitDataReady" : false,
"Name" : “aaaa”,
"exitDate" : null,
"date" : "2019-08-14T00:00:00+03:00",
"CreatedDate" : "2020-03-05T10:03:40.000+0000",
"Id" : “00000000”,
}
]
}
}
]
}
]
I tried:
To add on $match {'accounts_info.subAccounts.items': {$exist : true}}
But I did not get any data back at all.
To add on $match {'accounts_info.subAccounts.items.itemId': {$gt : 0}}
But I did not get any data back at all.

How to find and update an array in the object of array document using mongoose

A query to how to update the items array of object by
finding the breed dachshund
update the name to new-dog
updatedAt to the current time
[{
"_id" : ObjectId("60053b74aa72f132cb75b8b6"),
"clientId" : "test",
"items" : [
{
"_id" : ObjectId("60053b74aa72f132cb75b8b7"),
"name" : "tommy",
"breed" : "dachshund",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
},
{
"_id" : ObjectId("60053b74aa72f132cb75b8b8"),
"name" : "mickey",
"breed" : "husky",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
},
{
"_id" : ObjectId("60053b74aa72f132cb75b8b9"),
"name" : "whiskey",
"breed" : "dachshund",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
},
{
"_id" : ObjectId("60053b74aa72f132cb75b8ba"),
"name" : "milo",
"breed" : "bulldog",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
},
{
"_id" : ObjectId("60053b74aa72f132cb75b8bb"),
"name" : "pooh",
"breed" : "poodle",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
}
],
"shopname" : "myshopify"
}]
const curDatetime = moment(moment();
model.updateOne(
{_id: ObjectId("60053b74aa72f132cb75b8b6")},
{
arrayFilters: [
{'el.breed': 'dachshund'}
]
},
{
$set: {
'items.$[el].name': 'new-dog',
'items.$[el].updatedAt': curDatetime
}
}, "multi": true)

$project $lookup value not shown after $group

I have 2 related collection which i want to do a $lookup.
switches
{
"_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"relay" : NumberInt(1),
"name" : "Lampu Tengah",
"voltage" : 80.0,
"duration" : null,
"status" : true,
"triggered_by" : ObjectId("5e5fd642fce106005319e884"),
"created_at" : ISODate("2020-04-01T15:41:36.588+0000"),
"updated_at" : ISODate("2020-04-01T22:59:39.261+0000")
}
{
"_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"relay" : NumberInt(2),
"name" : "Kipas Angin",
"voltage" : 100.0,
"duration" : null,
"status" : true,
"triggered_by" : ObjectId("5e5fd642fce106005319e884"),
"created_at" : ISODate("2020-04-01T15:45:48.099+0000"),
"updated_at" : ISODate("2020-04-01T15:45:48.099+0000")
}
power_usage_month
{
"_id" : ObjectId("5e87edffffba850e8d72ce27"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 19.345,
"time_minutes" : NumberInt(123),
"created_at" : ISODate("2020-04-02T15:01:37.521+0000"),
"updated_at" : ISODate("2020-04-02T15:01:37.521+0000")
}
{
"_id" : ObjectId("5e87ee06ffba850e8d72ce28"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 17.5,
"time_minutes" : NumberInt(123),
"created_at" : ISODate("2020-04-03T20:35:09.870+0000"),
"updated_at" : ISODate("2020-04-03T20:35:09.871+0000")
}
{
"_id" : ObjectId("5e87ee0cffba850e8d72ce29"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 19.345,
"time_minutes" : NumberInt(124),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee13ffba850e8d72ce2a"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 17.5,
"time_minutes" : NumberInt(124),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee18ffba850e8d72ce2b"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 19.345,
"time_minutes" : NumberInt(125),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee20ffba850e8d72ce2c"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 17.5,
"time_minutes" : NumberInt(125),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee26ffba850e8d72ce2d"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 19.345,
"time_minutes" : NumberInt(126),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee2dffba850e8d72ce2e"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 17.5,
"time_minutes" : NumberInt(126),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
Now i want to "join" these collections using $lookup and $project the field i want to show, but apparently the $project value won't show after i do a $group. Here's my query
db.getCollection("power_usages_month").aggregate(
[
{
"$project" : {
"_id" : NumberInt(0),
"power_usages_month" : "$$ROOT"
}
},
{
"$lookup" : {
"localField" : "power_usages_month.switch_id",
"from" : "switches",
"foreignField" : "_id",
"as" : "switches"
}
},
{
"$unwind" : {
"path" : "$switches",
"preserveNullAndEmptyArrays" : false
}
},
{
"$group" : {
"_id" : "$power_usages_month.switch_id",
"sum_current" : {
"$sum" : "$power_usages_month.current"
}
}
},
{
"$project" : {
"switch_id" : "$_id",
"device_id" : "$switches.device_id",
"sum_current" : "$sum_current",
"voltage" : "$switches.voltage",
}
}
],
{
"allowDiskUse" : true
}
);
The "device_id" : "$switches.device_id", and "voltage" : "$switches.voltage" cannot appear as expected according to the result:
{
"_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"sum_current" : 70.0
}
{
"_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"sum_current" : 77.38
}
How could it be? Please tell me the fault of the query..
Edit: Here's my desired result
{
"_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"sum_current" : 70.0,
"voltage" : 80.0
}
{
"_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"sum_current" : 77.38,
"voltage" : 100.0
}
you can do the project as the last step after grouping and $lookup
also you can handle all of that in the group,
what you will include in the $group will be only available with you in the query,
but lets say you need to gather the power_usages_month docs used by each switch in one array in that switch,
but you don't need all the info from that power_usages_month, you just need the current and the time_minutes for example
you can do something like that
db.powerUsageMonth.aggregate(
[
{
$match: {}
},
{
$lookup: {
from: 'switches',
localField: 'switch_id',
foreignField: '_id',
as: 'switch'
}
},
{
$unwind: '$switch'
},
{
$group: {
_id: '$switch._id',
sum_current: {
$sum: '$current'
},
switchInfo: {
$first: '$switch'
},
powerUsageDocs: {
$addToSet: '$$ROOT'
}
}
},
{
$project: {
_id: 1,
sum_current: 1,
switchInfo: 1,
'powerUsageDocs.current': 1,
'powerUsageDocs.time_minutes': 1,
}
}
]
)
this will return an array of switches with their total current and the switch info (you can do some projection here too if you don't need all the switch info), and the power usages docs related to this switch (after projection)
the result will be something like that
{
"_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"sum_current" : 70,
"switchInfo" : {
"_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"relay" : 2,
"name" : "Kipas Angin",
"voltage" : 100,
"duration" : null,
"status" : true,
"triggered_by" : ObjectId("5e5fd642fce106005319e884"),
"created_at" : ISODate("2020-04-01T15:45:48.099Z"),
"updated_at" : ISODate("2020-04-01T15:45:48.099Z")
},
"powerUsageDocs" : [
{
"current" : 17.5,
"time_minutes" : 125
},
{
"current" : 17.5,
"time_minutes" : 123
},
{
"current" : 17.5,
"time_minutes" : 126
},
{
"current" : 17.5,
"time_minutes" : 124
}
]
}
{
"_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"sum_current" : 77.38,
"switchInfo" : {
"_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"relay" : 1,
"name" : "Lampu Tengah",
"voltage" : 80,
"duration" : null,
"status" : true,
"triggered_by" : ObjectId("5e5fd642fce106005319e884"),
"created_at" : ISODate("2020-04-01T15:41:36.588Z"),
"updated_at" : ISODate("2020-04-01T22:59:39.261Z")
},
"powerUsageDocs" : [
{
"current" : 19.345,
"time_minutes" : 124
},
{
"current" : 19.345,
"time_minutes" : 126
},
{
"current" : 19.345,
"time_minutes" : 123
},
{
"current" : 19.345,
"time_minutes" : 125
}
]
}
Update
If you just need info about your switch,
also you need to get the watt which is = total current * voltage of each switch,
we can do that in the $project, we can add the $multiply operator to the $project pipeline, which multiplies the voltage of the switch by the total current we just calculated in the $group pipeline
db.powerUsageMonth.aggregate(
[
{
$match: {}
},
{
$lookup: {
from: 'switches',
localField: 'switch_id',
foreignField: '_id',
as: 'switch'
}
},
{
$unwind: '$switch'
},
{
$group: {
_id: '$switch._id',
switch_id: { $first: '$switch._id' },
device_id: { $first: '$switch.device_id' },
voltage: { $first: '$switch.voltage' },
sum_current: {
$sum: '$current'
}
}
},
{
$project: {
_id: 1,
switch_id: 1,
device_id: 1,
voltage: 1,
sum_current: 1,
watt: { $multiply: [ "$voltage", "$sum_current" ] }
}
}
]
)
this will result an array of the form
{
"_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"voltage" : 100,
"sum_current" : 70,
"watt" : 7000
}
{
"_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"voltage" : 80,
"sum_current" : 77.38,
"watt" : 6190.4
}

Group into group and count using aggregation framework in MongoDB

I am trying to group data within group with the count.
{
"_id" : ObjectId("59899846b5f5670840040b0b"),
"client_id" : "merlin",
"scope" : "eee",
"user_id" : "u",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T10:53:58.816Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("59899849b5f5670840040b0c"),
"client_id" : "merlin",
"scope" : "eee",
"user_id" : "u",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T10:54:01.908Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989984db5f5670840040b0d"),
"client_id" : "merlin",
"scope" : "eee",
"user_id" : "y",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T10:54:05.280Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989adb2d699bd211caa07ad"),
"client_id" : "symphony",
"scope" : "eee",
"user_id" : "q",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T12:25:22.518Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989adb8d699bd211caa07ae"),
"client_id" : "symphony",
"scope" : "eee",
"user_id" : "w",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T12:25:28.954Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989adbcd699bd211caa07af"),
"client_id" : "symphony",
"scope" : "eee",
"user_id" : "q",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T12:25:32.753Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989adc0d699bd211caa07b0"),
"client_id" : "symphony",
"scope" : "eee",
"user_id" : "r",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T12:25:36.176Z"),
"open_url" : null,
"__v" : 0
}
What i want is to group data on the basis of client_id and further on the basis of user_id to get the count of documents.
i have tried this:-
Logger.aggregate([
{ "$group": {
"_id": {client: "$client_id"},
"count": { "$sum": 1 }
}}
])
I am getting:-
client: merlin
count: 3,
client: symphony,
count: 4
But how to group this on the basis of user_id.
I want the final output would be:-
client: merlin,
count: 2,
client: symphony,
count: 3
Here is the query.
db.collection.aggregate([
{ "$group": {
"_id": {client: "$client_id" },
"clientIdCount": { "$sum": 1 },
"userids" : {"$addToSet" : "$user_id"}
}
},
{ "$project" : {"_id.client" : 1, "clientIdCount" : 1, numberOfUsers : {$size : "$userids"} }
}
]);
Output:-
If you don't want clientIdCount in the output, just change the value from 1 to 0 present next to clientIdCount.
/* 1 */
{
"_id" : {
"client" : "symphony"
},
"clientIdCount" : 4,
"numberOfUsers" : 3
}
/* 2 */
{
"_id" : {
"client" : "merlin"
},
"clientIdCount" : 3,
"numberOfUsers" : 2
}
If I understood your question correctly this your db query
db.Logger.aggregate([
{
$group : {
_id : "$client_id", user : { $push: "$user_id" },
count: { $sum: 1 }
}
}
])

Query nested, 2 leves, array of objects in MongoDB

I am trying to query a coolection that has the following structure:
{
"_id" : ObjectId("58eed22d09865610c23453e3"),
"name" : "Maria das Dores",
"type" : "P",
"nickname" : "Dolores",
"notes" : "Notas\r\n\r\nCom quebra de página",
"updated_at" : ISODate("2017-04-13T01:19:41.000Z"),
"created_at" : ISODate("2017-04-13T01:19:41.000Z"),
"emails" : [
{
"value" : "maria#dores.com",
"default" : true,
"updated_at" : ISODate("2017-04-13T01:19:41.000Z"),
"created_at" : ISODate("2017-04-13T01:19:41.000Z"),
"_id" : ObjectId("58eed22d09865610c23453e4")
},
{
"value" : "maria#semdores.com",
"_id" : ObjectId("58eed23d09865605614005c4"),
"updated_at" : ISODate("2017-04-13T01:19:57.000Z"),
"created_at" : ISODate("2017-04-13T01:19:57.000Z")
}
],
"phones" : [
{
"value" : "(33) 8282383-2933",
"default" : false,
"updated_at" : ISODate("2017-04-25T12:11:14.000Z"),
"created_at" : ISODate("2017-04-13T01:19:41.000Z"),
"_id" : ObjectId("58eed22d09865610c23453e5")
},
{
"value" : "(85) 101010-1010101",
"default" : true,
"_id" : ObjectId("58ff3ce209865605681f40c2"),
"updated_at" : ISODate("2017-04-25T12:11:14.000Z"),
"created_at" : ISODate("2017-04-25T12:11:14.000Z")
},
{
"value" : "21343243343",
"_id" : ObjectId("58ff3d7d0986560b861c3b32"),
"updated_at" : ISODate("2017-04-25T12:13:49.000Z"),
"created_at" : ISODate("2017-04-25T12:13:49.000Z")
}
],
"copartner" : {
"enabled" : true,
"updated_at" : ISODate("2017-05-17T00:32:42.000Z"),
"created_at" : ISODate("2017-05-11T02:35:40.000Z"),
"_id" : ObjectId("5913cdfc09865664df031ec2"),
"applications" : {
"0" : {
"application_id" : "58e46443098656283d225b52",
"responsibility" : "DEV",
"percentage" : 1250,
"_id" : ObjectId("591a6f9d0986563c174cefd3"),
"updated_at" : ISODate("2017-05-16T03:18:53.000Z"),
"created_at" : ISODate("2017-05-16T03:18:53.000Z")
},
"1" : {
"application_id" : "58e46443098656283d225b52",
"responsibility" : "SALE",
"percentage" : 2000,
"_id" : ObjectId("591b9a2a09865605697fe3e3"),
"updated_at" : ISODate("2017-05-17T00:32:42.000Z"),
"created_at" : ISODate("2017-05-17T00:32:42.000Z")
}
}
}
}
I already test with the "dot" notation and using $elemMatch, but no one of them result in any match.
Dot notation query:
db.getCollection('persons').find({
"copartner.applications.application_id": "58e46443098656283d225b52"
})
$elemMatch query:
db.getCollection('persons').find({
"copartner.applications": { $elemMatch: { "applications_id": "58e46443098656283d225b52" } }
})
The dot notation will work with either one of these queries:
db.getCollection('persons').find({
"copartner.applications.0.application_id": "58e46443098656283d225b52"})
AND
db.getCollection('persons').find({
"copartner.applications.1.application_id": "58e46443098656283d225b52"})
"applications" is not an array, so $elemMatch is not applicable. "0" and "1" are nested fields instead of array elements. If you can, you're better off using an array for "applications".