Filter day on MongoDB - mongodb

I have a collection called "batches" that has the field "createdAt" with ISODate:
{
"_id" : ObjectId("5f6134ecf908840018f2e9ea"),
"status" : "BATCH_PROCESSING",
"groupId" : 13,
"type" : "not",
"finished" : false,
"scheduledTo" : "2020-09-15T18:50:51-03:00",
"identifier" : "COLABORADORES",
"createdAt" : "2020-09-18T18:41:00-03:00",
"__v" : 0
}
{
"_id" : ObjectId("5f6144d7863bb40018045906"),
"status" : "BATCH_PROCESSING",
"groupId" : 13,
"type" : "not",
"finished" : false,
"scheduledTo" : "2020-09-16T19:48:00-03:00",
"identifier" : "COLABORADORES",
"createdAt" : "2020-09-15T19:48:55-03:00",
"__v" : 0
}
{
"_id" : ObjectId("5f617021b985c1001974c3af"),
"status" : "CANCELED",
"groupId" : 13,
"type" : "not",
"finished" : true,
"scheduledTo" : "2020-09-15T23:06:00-03:00",
"identifier" : "COLABORADORES",
"createdAt" : "2020-09-18T22:53:37-03:00",
"__v" : 0,
"numberOfLines" : 1
}
I need to filter only documents in a specific date. I tried many ways but it didn't bring the documents :
db.getCollection("batches").find(
{ createdAt : {
"$gte": ISODate("2020-09-15T00:00:00Z"),
"$lt": ISODate("2020-09-19T00:00:00Z")
} }
);
db.getCollection("batches").find({ createdAt : Date("2020-09-15")}
);
The query works well, but the answer is always:
Fetched 0 record(s) in 136ms
How do I do this filter correctly?

Your data is malformed. Your field 'createdDate' is a string, not an ISODate(). Consider this...
db.batches.insertMany([
{
"_id" : ObjectId("5f6134ecf908840018f2e9ea"),
"status" : "BATCH_PROCESSING",
"groupId" : 13,
"type" : "not",
"finished" : false,
"scheduledTo" : "2020-09-15T18:50:51-03:00",
"identifier" : "COLABORADORES",
"createdAt" : ISODate("2020-09-18T18:41:00-03:00"),
"__v" : 0
},
{
"_id" : ObjectId("5f6144d7863bb40018045906"),
"status" : "BATCH_PROCESSING",
"groupId" : 13,
"type" : "not",
"finished" : false,
"scheduledTo" : "2020-09-16T19:48:00-03:00",
"identifier" : "COLABORADORES",
"createdAt" : ISODate("2020-09-15T19:48:55-03:00"),
"__v" : 0
},
{
"_id" : ObjectId("5f617021b985c1001974c3af"),
"status" : "CANCELED",
"groupId" : 13,
"type" : "not",
"finished" : true,
"scheduledTo" : "2020-09-15T23:06:00-03:00",
"identifier" : "COLABORADORES",
"createdAt" : ISODate("2020-09-18T22:53:37-03:00"),
"__v" : 0,
"numberOfLines" : 1
}]
)
Query
When applying the query in your post...
db.getCollection("batches").find(
{ createdAt : {
"$gte": ISODate("2020-09-15T00:00:00Z"),
"$lt": ISODate("2020-09-19T00:00:00Z")
} })
... we now see the following results:
MongoDB Enterprise replSet:PRIMARY> db.getCollection("batches").find(
... { createdAt : {
... "$gte": ISODate("2020-09-15T00:00:00Z"),
... "$lt": ISODate("2020-09-19T00:00:00Z")
... } })
{ "_id" : ObjectId("5f6134ecf908840018f2e9ea"), "status" : "BATCH_PROCESSING", "groupId" : 13, "type" : "not", "finished" : false, "scheduledTo" : "2020-09-15T18:50:51-03:00", "identifier" : "COLABORADORES", "createdAt" : ISODate("2020-09-18T21:41:00Z"), "__v" : 0 }
{ "_id" : ObjectId("5f6144d7863bb40018045906"), "status" : "BATCH_PROCESSING", "groupId" : 13, "type" : "not", "finished" : false, "scheduledTo" : "2020-09-16T19:48:00-03:00", "identifier" : "COLABORADORES", "createdAt" : ISODate("2020-09-15T22:48:55Z"), "__v" : 0 }

Related

Deleting an object inside another object in Mongodb and for GOlang

I wanna delete a specific object inside an object, My object:
{
"_id" : ObjectId("5f577f3cce031ee00f5e32c9"),
"UserId" : 0,
"firstname" : "user 1",
"lastname" : "user 1",
"finishedTrainings" : [
{
"itemId" : 3,
"validationScore" : 1,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:57:31.275Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
},
{
"itemId" : 0,
"validationScore" : 0.6666666666666666,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:59:04.268Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
}
],
"biography" : ""
}
and I wanna delete the finishedTraining(id=3),what would be the syntax in mongodb
//code output from mongo shell 4.2, windows10
//data set as given in problem statement
> db.userTraining.find().pretty();
{
"_id" : ObjectId("5f577f3cce031ee00f5e32c9"),
"UserId" : 0,
"firstname" : "user 1",
"lastname" : "user 1",
"finishedTrainings" : [
{
"itemId" : 3,
"validationScore" : 1,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:57:31.275Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
},
{
"itemId" : 0,
"validationScore" : 0.6666666666666666,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:59:04.268Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
}
],
"biography" : ""
}
//assuming objectID is unique for the document, you can query your own business field
> db.userTraining.update(
... {_id:ObjectId("5f577f3cce031ee00f5e32c9")},
... {$pull:{"finishedTrainings":{itemId:3}}},
... false,
... true
... );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.userTraining.find().pretty();
{
"_id" : ObjectId("5f577f3cce031ee00f5e32c9"),
"UserId" : 0,
"firstname" : "user 1",
"lastname" : "user 1",
"finishedTrainings" : [
{
"itemId" : 0,
"validationScore" : 0.6666666666666666,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:59:04.268Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
}
],
"biography" : ""
}
>
//above id=3 is deleted object from the array, other document object remain intact by using "$pull" in the update statement

MongoDB : lookup exact match without false rows

I have 3 tables :
accounts:[
{id:111,name:'john'},
{id:222,name:'due'}
]
workoutTypes:[
{id:1,title:'a'},
{id:2,title:'b'}
]
accountrecords:[
{_id:10,workoutTypeId:1,accountId:111},
{_id:11,workoutTypeId:1,accountId:222}
]
I used this command to group all account records by workout type that match to the current account like that:
db.workoutTypes.aggregate([
{
$lookup: {
from: "accountrecords",
localField: "_id",
foreignField: "workoutTypeId",
as: "records"
}
},
{
$match: {
$and: [
{ "records": { $ne: [] } },
{ "records.accountId": { $eq: mongoose.Types.ObjectId(accountId) } },
]
}
},
])
the issue that I'm getting the records that related to both of accounts.
saw that eq command return a false value for non-match values.
there is an option to get only the record that matches to current account?
First of all your localField on workoutTypes collection must be id, not _id. Probably a typo from your side. Secondly, you need to use $redact operator if you want to restrict the content. $match actually works as expected, but it actually isn't checking whether all your sub documents are satisfying the criteria.
Update:
Try this query:
db.workoutTypes.aggregate([
{
$lookup: {
from: "accountrecords",
localField: "_id",
foreignField: "workoutTypeId",
as: "records"
}
},
{$match:{"records.accountId":{$exists:true}, "records.accountId":{$eq:ObjectId("5a604b18280420c03e8d3a23")}}},
{$redact:{
$cond:{
if:{$or:[{$eq:["$accountId",ObjectId("5a604b18280420c03e8d3a23")]},{$not:"$accountId"}]},
then:"$$DESCEND",
else:"$$PRUNE"
}
}}
])
And the output is:
/* 1 */
{
"_id" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"updatedAt" : ISODate("2018-04-07T11:59:52.583Z"),
"createdAt" : ISODate("2018-04-07T11:59:52.583Z"),
"title" : "Snatch",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5acb1b666b08493b30deed92"),
"updatedAt" : ISODate("2018-04-09T07:51:02.294Z"),
"createdAt" : ISODate("2018-04-09T07:51:02.294Z"),
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"repetition" : 4,
"weight" : 120,
"__v" : 0
},
{
"_id" : ObjectId("5acb1b6f6b08493b30deed93"),
"updatedAt" : ISODate("2018-04-09T07:51:11.878Z"),
"createdAt" : ISODate("2018-04-09T07:51:11.878Z"),
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"repetition" : 5,
"weight" : 130,
"__v" : 0
},
{
"_id" : ObjectId("5ace23aa1c4d590dc049e94e"),
"updatedAt" : ISODate("2018-04-11T15:03:06.824Z"),
"createdAt" : ISODate("2018-04-11T15:03:06.824Z"),
"repetition" : 124,
"weight" : 32,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"__v" : 0
},
{
"_id" : ObjectId("5ace23b21c4d590dc049e94f"),
"updatedAt" : ISODate("2018-04-11T15:03:14.293Z"),
"createdAt" : ISODate("2018-04-11T15:03:14.293Z"),
"repetition" : 325,
"weight" : 235,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"__v" : 0
},
{
"_id" : ObjectId("5acc8ef5345125417cca18e3"),
"updatedAt" : ISODate("2018-04-10T10:16:21.709Z"),
"createdAt" : ISODate("2018-04-10T10:16:21.709Z"),
"repetition" : 123,
"weight" : 456,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"__v" : 0
}
]
}
/* 2 */
{
"_id" : ObjectId("5acdf56afd5ca008d01b706f"),
"updatedAt" : ISODate("2018-04-11T11:45:46.192Z"),
"createdAt" : ISODate("2018-04-11T11:45:46.192Z"),
"title" : "Jerk",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5acdf597fd5ca008d01b7070"),
"updatedAt" : ISODate("2018-04-11T11:46:31.674Z"),
"createdAt" : ISODate("2018-04-11T11:46:31.674Z"),
"repetition" : 12,
"weight" : 100,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5acdf56afd5ca008d01b706f"),
"__v" : 0
}
]
}
/* 3 */
{
"_id" : ObjectId("5ace336ed36c780e6ce76d5d"),
"updatedAt" : ISODate("2018-04-11T16:10:22.483Z"),
"createdAt" : ISODate("2018-04-11T16:10:22.483Z"),
"title" : "Weight lifting",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5ace337ed36c780e6ce76d5e"),
"updatedAt" : ISODate("2018-04-11T16:10:38.708Z"),
"createdAt" : ISODate("2018-04-11T16:10:38.708Z"),
"repetition" : 21,
"weight" : 100,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ace336ed36c780e6ce76d5d"),
"__v" : 0
}
]
}
/* 4 */
{
"_id" : ObjectId("5ac8cf5ef14469169d8627e8"),
"updatedAt" : ISODate("2018-04-07T14:02:06.155Z"),
"createdAt" : ISODate("2018-04-07T14:02:06.155Z"),
"title" : "Double under",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5acc8f11345125417cca18e4"),
"updatedAt" : ISODate("2018-04-10T10:16:49.069Z"),
"createdAt" : ISODate("2018-04-10T10:16:49.069Z"),
"repetition" : 111111,
"weight" : 222222,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8cf5ef14469169d8627e8"),
"__v" : 0
},
{
"_id" : ObjectId("5ace2d7b1c4d590dc049e950"),
"updatedAt" : ISODate("2018-04-11T15:44:59.722Z"),
"createdAt" : ISODate("2018-04-11T15:44:59.722Z"),
"weight" : 23,
"repetition" : 123555555,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8cf5ef14469169d8627e8"),
"__v" : 0
}
]
}
/* 5 */
{
"_id" : ObjectId("5ac8cfdd79a48216b3e048b0"),
"updatedAt" : ISODate("2018-04-07T14:04:13.670Z"),
"createdAt" : ISODate("2018-04-07T14:04:13.670Z"),
"title" : "Push-ups",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5accc1f1cd58a407c6562f79"),
"updatedAt" : ISODate("2018-04-10T13:53:53.933Z"),
"createdAt" : ISODate("2018-04-10T13:53:53.933Z"),
"repetition" : 123,
"weight" : 456000,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8cfdd79a48216b3e048b0"),
"__v" : 0
}
]
}
Now you just replace ObjectId("5a604b18280420c03e8d3a23") in the query with your mongoose code mongoose.Types.ObjectId(accountId) and you should be good now.

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".

Get all the distinct users through mongodb aggregation

I have an collection for messages something like below. Now i want to get all the distinct users to which ObjectId("57f55e4799aabf1c0565bc10") messages or receive message excluding itself (ObjectId("57f55e4799aabf1c0565bc10")). So how i can achieve it using aggregation.
/* 1 */
{
"_id" : ObjectId("57f55e0199aabf1c0565bc0e"),
"message" : "fghjfghj",
"to" : ObjectId("57f54e154df8d0193577889a"),
"from" : ObjectId("57f5522099aabf1c0565bbfe"),
"modifiedDate" : ISODate("2016-10-05T20:09:37.307Z"),
"createdDate" : ISODate("2016-10-05T20:09:37.307Z"),
"isDeleted" : false,
"status" : true,
"type" : "Message",
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("57f55e2d99aabf1c0565bc0f"),
"message" : "ghjkfhkg",
"to" : ObjectId("57f54e154df8d0193577889a"),
"from" : ObjectId("57f5522099aabf1c0565bbfe"),
"modifiedDate" : ISODate("2016-10-05T20:10:21.245Z"),
"createdDate" : ISODate("2016-10-05T20:10:21.245Z"),
"isDeleted" : false,
"status" : true,
"type" : "Message",
"__v" : 0
}
/* 3 */
{
"_id" : ObjectId("57f55e4799aabf1c0565bc10"),
"message" : "hgkgh",
"to" : ObjectId("57f54e154df8d0193577889a"),
"from" : ObjectId("57f5522099aabf1c0565bbfe"),
"modifiedDate" : ISODate("2016-10-05T20:10:47.614Z"),
"createdDate" : ISODate("2016-10-05T20:10:47.614Z"),
"isDeleted" : false,
"status" : true,
"type" : "Message",
"__v" : 0
}
/* 4 */
{
"_id" : ObjectId("57f55e7199aabf1c0565bc11"),
"message" : "cbjngcfj",
"to" : ObjectId("57f54e154df8d0193577889a"),
"from" : ObjectId("57f5522099aabf1c0565bbfe"),
"modifiedDate" : ISODate("2016-10-05T20:11:29.197Z"),
"createdDate" : ISODate("2016-10-05T20:11:29.197Z"),
"isDeleted" : false,
"status" : true,
"type" : "Message",
"__v" : 0
}
/* 5 */
{
"_id" : ObjectId("57f5801d94ef64d760275368"),
"message" : "cbjngcfj",
"from" : ObjectId("57f54e154df8d0193577889a"),
"to" : ObjectId("57f5522099aabf1c0565bbfe"),
"modifiedDate" : ISODate("2016-10-05T20:11:29.197Z"),
"createdDate" : ISODate("2016-10-05T20:11:29.197Z"),
"isDeleted" : false,
"status" : true,
"type" : "Message",
"__v" : 0
}
/* 6 */
{
"_id" : ObjectId("57f5865494ef64d76027536a"),
"message" : "hgkgh",
"to" : ObjectId("57f54e154df8d0193577889b"),
"from" : ObjectId("57f5522099aabf1c0565bbfe"),
"modifiedDate" : ISODate("2016-10-05T20:10:47.614Z"),
"createdDate" : ISODate("2016-10-05T20:10:47.614Z"),
"isDeleted" : false,
"status" : true,
"type" : "Message",
"__v" : 0
}
/* 7 */
{
"_id" : ObjectId("57f666de7d13f10b5aa53527"),
"message" : "Zsdfsdf",
"to" : ObjectId("57f54dd36df11022ac5d7769"),
"from" : ObjectId("57f54dd36df11022ac5d7769"),
"modifiedDate" : ISODate("2016-10-06T14:59:42.943Z"),
"createdDate" : ISODate("2016-10-06T14:59:42.943Z"),
"isDeleted" : false,
"status" : true,
"type" : "Message",
"__v" : 0
}
/* 8 */
{
"_id" : ObjectId("57f667397d13f10b5aa53528"),
"message" : "HIIIIIIIIIIIII",
"to" : ObjectId("57f54dd36df11022ac5d7769"),
"from" : ObjectId("57f54dd36df11022ac5d7769"),
"modifiedDate" : ISODate("2016-10-06T15:01:13.993Z"),
"createdDate" : ISODate("2016-10-06T15:01:13.993Z"),
"isDeleted" : false,
"status" : true,
"type" : "Message",
"__v" : 0
}
/* 9 */
{
"_id" : ObjectId("57f66972a533c40d67ebfab8"),
"message" : "gfjghk",
"to" : ObjectId("57f54e154df8d0193577889a"),
"from" : ObjectId("57f5522099aabf1c0565bbfe"),
"modifiedDate" : ISODate("2016-10-06T15:10:42.721Z"),
"createdDate" : ISODate("2016-10-06T15:10:42.721Z"),
"isDeleted" : false,
"status" : true,
"type" : "Message",
"__v" : 0
}
I have tried but didn't able to get the required result as below :
db.getCollection('conversations').aggregate([
{$match : {'type' : 'Message',
$or : [
{from : ObjectId("57f5522099aabf1c0565bbfe")},
{to : ObjectId("57f5522099aabf1c0565bbfe")}
]
}
},
{ $group: {_id : {"to" : "$to", 'from':"$from" }}},
]);
Expecting results like : -
[
ObjectId("57f54e154df8d0193577889a")
ObjectId("57f54e154df8d0193577889b")
]
Thanks for any suggestion.
You can use the $addToSet to add the senders and receivers to their respective sets(lists). Then you can use $setUnion to combine the sets. Then, you can use $setDifference to remove the current user.
db.getCollection('conversations').aggregate([
{$match : {'type' : 'Message',$or : [ {from : ObjectId("57f5522099aabf1c0565bbfe")},
{to : ObjectId("57f5522099aabf1c0565bbfe")}]}},
{ $group: {_id : null, from : {"$addToSet" : "$from"}, to : {"$addToSet" : "$to"}}},
{"$project":{"userAll":{"$setUnion":["$from", "$to"]}}},
{"$project":{"_id":0, users: {"$setDifference":["$userAll", [ObjectId("57f5522099aabf1c0565bbfe")]]}}}
]);
Sample output:
{
"users" : [
ObjectId("57f54e154df8d0193577889b"),
ObjectId("57f54e154df8d0193577889a")
]
}

Inconsistent query results with embedded documents on MongoDB

I've got a collection called payments with an example of its document shown below:
{
"_id" : ObjectId("579b5ee817e3aaac2f0aebc1"),
"updatedAt" : ISODate("2016-07-29T11:04:01.209-03:00"),
"createdAt" : ISODate("2016-07-29T10:49:28.113-03:00"),
"createdBy" : ObjectId("5763f56010cd7b03008147d4"),
"contract" : ObjectId("578cb907f1575f0300d84d09"),
"recurrence" : [
{
"when" : ISODate("2016-05-29T11:03:45.606-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2d"),
"transaction" : {
"createdAt" : ISODate("2016-05-29T11:03:45.608-03:00"),
"tid" : "9999999999999999B01A",
"status" : 4,
"code" : "00",
"message" : "Transação autorizada"
},
"status" : "PAGO"
},
{
"when" : ISODate("2016-06-29T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2c"),
"transaction" : {
"createdAt" : ISODate("2016-06-29T11:03:45.608-03:00"),
"tid" : "9999999999999999B01A",
"status" : 4,
"code" : "00",
"message" : "Transação autorizada"
},
"status" : "PAGO"
},
{
"when" : ISODate("2016-07-29T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2b"),
"status" : "ERRO",
"transaction" : {
"code" : "56",
"createdAt" : ISODate("2016-07-29T11:04:01.196-03:00"),
"message" : "Autorização negada",
"status" : 5,
"tid" : "1006993069000730B88A"
}
},
{
"when" : ISODate("2016-07-30T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2a"),
"status" : "PENDENTE"
},
{
"when" : ISODate("2016-07-31T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e29"),
"status" : "PENDENTE"
},
{
"when" : ISODate("2016-08-01T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e28"),
"status" : "PENDENTE"
}
],
"status" : "PAGO",
"conditions" : {
"originalValue" : 7406.64,
"totalValue" : 7400,
"upfrontValue" : 1500,
"upfrontInstallments" : 3,
"balanceInstallments" : 9
},
"__v" : 0,
"transaction" : {
"code" : "00",
"createdAt" : ISODate("2016-07-29T10:49:46.610-03:00"),
"message" : "Transação autorizada",
"status" : 6,
"tid" : "1006993069000730AF5A"
}
}
If I run the query below, I get the desired document shown above:
db.payments.find({ "recurrence.transaction.tid": "1006993069000730B88A" })
However, if I run this other query, MongoDB returns my entire collection (presumably because it didn't match the subdocument's id):
db.payments.find({ "recurrence._id": ObjectId("579b6241ea945e3631f64e2b") })
Both queries should return the same result! I also checked some other questions including this one so unless I'm going crazy I'm doing the same thing. Not sure why the inconsistent results though.
Tryout this:
db.payments.find({ recurrence : { $elemMatch: { "transaction.tid": "1006993069000730B88A"} } }).pretty()