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()
Related
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 }
I use mongoose and I have this data in my mongodb :
"_id" : ObjectId("5f13e1edf7c56a896987c191"),
"deleted" : false,
"email" : "klinikkoding#gmail.com",
"firstName" : "Klinik",
"lastName" : "Koding",
"resetToken" : null,
"workspaces" : [
{
"status" : "active",
"_id" : ObjectId("5f13e124f7c56a896987c18e"),
"code" : "kk",
"name" : "Klinik Koding",
"_roleId" : ObjectId("5f13de3eb33fa33ce2a3b0dd")
},
{
"status" : "invited",
"_id" : ObjectId("5f13e13ff7c56a896987c190"),
"code" : "rm",
"name" : "The Manage",
"_roleId" : ObjectId("5f13de3eb33fa33ce2a3b0dd")
}
],
How can I return only one workspace? The output that I need is like this:
"_id" : ObjectId("5f13e1edf7c56a896987c191"),
"deleted" : false,
"email" : "klinikkoding#gmail.com",
"firstName" : "Klinik",
"lastName" : "Koding",
"resetToken" : null,
"workspaces" : [
{
"status" : "active",
"_id" : ObjectId("5f13e124f7c56a896987c18e"),
"code" : "kk",
"name" : "Klinik Koding",
"_roleId" : ObjectId("5f13de3eb33fa33ce2a3b0dd")
},
],
Anyone can help me with this query?
Try this.
async function retrieve(){
// You can retrieve it by any field. I used email because it is unique.
let data=await yourModelName.findOne({email:"klinikkoding#gmail.com"})
data.workspaces.splice(1,1)
console.log("your final result",data)
}
retrieve()
I am pretty new to mongodb. I have 3 levels of documents.
Parent > Child > Child, all having _id field.
{
"_id" : "n2qw5sojs4bajrj",
"Title" : "Mr",
"Instance" : "HQ",
"FirstName" : "ppp",
"LastName" : "uuuu",
"Position" : "BF",
"EmailAddress" : "xxx#ppp.com",
"Requests" : [
{
"_id" : "121",
"Date" : "12/02/2018",
"Status" : "New",
"ApprovedBy" : {
"_id" : "sdfsdf",
"Name" : "MAN"
},
"PPE" : [
{
"_id" : "121",
"Code" : "PPE",
"Status" : "New",
"Title" : "Trousers",
"Type" : "STD",
"Size" : "10111116",
"Qty" : 1,
"LostDamage" : {
"Reason" : "asdaD",
"Location" : "Station",
"Damage" : "Damged"
}
},
{
"_id" : "122",
"Code" : "PPEOPP",
"Status" : "New",
"Title" : "TrousASDASDASDers",
"Type" : "STD",
"Size" : "10111116",
"Qty" : 1,
"LostDamage" : {
"Reason" : "asdaD",
"Location" : "Station",
"Damage" : "Damged"
}
}
]
}
]
}
I would like find out how to delete the last PPE Element (Parent > Request > PPE) by the _id column.
So I would to delete the following child:
{
"_id" : "121",
"Code" : "PPE",
"Status" : "New",
"Title" : "Trousers",
"Type" : "STD",
"Size" : "10111116",
"Qty" : 1,
"LostDamage" : {
"Reason" : "asdaD",
"Location" : "Station",
"Damage" : "Damged"
}
}
Any tips / help would be great.
Thanks
Paul
You can use MongoDb $pop to remove your last element from array. Like this go to https://docs.mongodb.com/manual/reference/operator/update/pop/
db.collection.update({id:1}, { $pop:{ 'request.PPE':1}});
[ "_id" : ObjectId("59d1f4d0539f772aacf90f61"),
"user_id" : 1222.0,
"user_name" : "jadenn",
"email" : "jdkdkdjjjjjjjjjjjjjjjjjjjh#j.com",
"phone" : 333333333.0,
"image_ref" : "static/image/123IMG_20170917_232813.jpg",
"stories" : [
{
"title" : "accident",
"description" : "kjsbskns",
"longitude" : 19.2,
"latitude" : 81.4,
"location" : "isl",
"date" : 12.0,
"reactions" : [
{
"type" : "seen",
"date" : "1234",
"user_id" : 123.0
},
{
"type" : "seen",
"date" : "1234",
"user_id" : 123.0
}
],
"comments" : [
{
"comment" : "djksnjknfkjguye",
"date" : 123.0,
"user_id" : 123.0
},
{
"comment" : "this is the accident case ",
"date" : 123.0,
"user_id" : 123.0
}
],
"reports" : [
{
"description" : "uye",
"date" : 123.0,
"user_id" : 1232321.0
}
],
"catagory_name" : [
{
"type" : "uye"
}
]
}
]
}
Here is my document I want a query which will return only list of stories. Not a complete document of user. I searched but could not find the solution. Every solution return complete document by applying "Where condition as MYSQL"]1
You need to project on stories key from your document as below:
db.youCollection.find({}, {stories:1, _id:0})
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".