MongoDB: Find a document that matches a condition in all its subdocument - mongodb

I am trying to write a mongo query to fetch the result that satisifes a condition on all its subdocument,
1. {
"_id" : 1,
"data" : [
{
"Id" : "513",
"Nom" : "alouale pfouga",
}
],
"campaigns" : [
{
"user" : "1",
"latest" : NumberInt(0),
"postcode" : [
[
]
],
},
{
"user" : "2",
"latest" : NumberInt(1),
"postcode" : [
{
"id" : "772",
"name" : "xxx",
}
],
}
],
}
2. {
"_id" : 2,
"data" : [
{
"Id" : "514",
"Nom" : "pfouga remi",
}
],
"campaigns" : [
{
"user" : "1",
"latest" : NumberInt(0),
"postcode" : [
[
]
],
},
],
}
I need to find the record that has "Postcode" array is empty.
As far as I understand query like this: db.users.find({"campaigns.postcode":[]}) gives me both records, but i need only record 2 , because record 1 has postcode node in one sub documents, Please anyone help me !!!

This would give you the answer:
db.test.find({ 'campaigns.postcode': { $size: 0 } } )
but in your case 'campaigns.postcode' has an empty array in it and that means the size is not 0, its 1
You should remove your empty array
"postcode" : [
[
]
],
should be
"postcode" : []
Updated:
After spending some time on the question I realized the problem
For converting "postcode" : [[]] to "postcode" : [] you can use this code:
db.test.find({ 'campaigns.postcode': []} ).forEach(function(item){
for(var i0 = 0; i0 < item.campaigns.length; i0++)
{
if(item.campaigns[i0].postcode[0].length == []) item.campaigns[i0].postcode = [];
}
db.test.update({'_id' : item._id}, item);
});
after that you can find your proper result with:
db.test.find({ 'campaigns.postcode': { $size: 0 }, 'campaigns': { $size: 1 } } )

In your json structure it contains
"postcode" : [
[
]
],
So I tried some but not found exact what you want in expected results I found postcode data where all postcode was empty using following aggregation
db.collectionName.aggregate({"$unwind":"$campaigns"},
{"$unwind":"$campaigns.postcode"},
{"$match":{"campaigns.postcode":{"$size":0}}})

Related

Sort query for dynamic array in Mongo DB

I have a collection in MongoDB like below format. Now i want to know how to apply sorting. Please find example of collection below,
{
"_id" : ObjectId("5e5e140f113a6c3970eef3bb"),
"FormId" : "5cd3a0a0cb20953208fcb549",
"FieldsDatas" : [
{
"FieldId" : "4fbcef5b-d60a-4908-a037-5ff085b70709",
"Value" : [
"202003031"
]
},
{
"FieldId" : "708e1baf-fcd0-45fa-b1de-27f34391c35c",
"Value" : [
"202003031"
]
},
{
"FieldId" : "0b563a80-2b0a-4803-ad7f-652a381e134c",
"Value" : [
"New Source",
"Endpoint"
]
},
{
"FieldId" : "15355b82-4fae-4c09-acb4-13f95e8c2d4e",
"Value" : [
"2020-03-03 13:51:17"
]
},
{
"FieldId" : "1e32a283-34a9-4a7b-b851-3e5ac7f93d2c",
"Value" : [
"tets"
]
}
]
}
{
"_id" : ObjectId("5e5e1c89113a6c3970eef3bc"),
"FormId" : "5cd3a0a0cb20953208fcb549"
"FieldsDatas" : [
{
"FieldId" : "708e1baf-fcd0-45fa-b1de-27f34391c35c",
"Value" : [
"202003032"
]
},
{
"FieldId" : "0eca0881-a69b-4db3-b8b1-d74b0a16d4ef",
"Value" : [
"20200303_2#mail.com"
]
},
{
"FieldId" : "2da67714-aaf3-433d-9a86-1b48c75470ec",
"Value" : [
"mani lj"
]
},
{
"FieldId" : "a0b26aac-cad0-4c5e-83b4-9a01ac4ce97a",
"Value" : []
},
{
"FieldId" : "15355b82-4fae-4c09-acb4-13f95e8c2d4e",
"Value" : [
"2020-03-03 14:29:23"
]
}
]
}
For above collection i have date inside the array of objects called FieldsDatas where "FieldId": "15355b82-4fae-4c09-acb4-13f95e8c2d4e" in that same "Value" array has datetime. I want to sort the result based in this datetime.
I have a filter query. How to add sort query for above scenario?
db.getCollection('Collection').find({
"$and":[
{
FieldsDatas: {
$elemMatch:{
FieldId:'955c9843-1535-4df8-a1c4-09430ac9f6ba',
Value: { $ne: ["Contact"] }
}
}
}
]
})
.limit(5)
.skip(30);
Anyone please help me to add sorting for this query like above datetime field.
Any possibilities to achieve this by Stored procedure or functions. Please let me know.
Thanks in advance,
Mani

in mongo, cannot perform $setIntersection on array with aggregate into project

I have this two collection which each document presents a chat session:
{
"_id" : ObjectId("58136ba83bdddd2d3cd4b3fb"),
"Created" : ISODate("2016-10-28T15:15:52.563Z"),
"Messages" : [
{
"Created" : ISODate("2016-10-28T15:15:52.567Z"),
"ReadBy" : [
ObjectId("57c96a14870ae36ede4d8085"),
ObjectId("57c972b6fc8effecde6cf0fa")
],
"Content" : "Hello"
}
]
}
{
"_id" : ObjectId("5813ac380a45415df8e7fc08"),
"Created" : ISODate("2016-10-28T15:16:52.563Z"),
"Messages" : [
{
"Created" : ISODate("2016-10-28T15:15:52.567Z"),
"ReadBy" : [
ObjectId("57c96a14870ae36ede4d8234"),
ObjectId("57c972b6fc8effecde6cf987")
],
"Content" : "Hello2"
}
]
}
I am trying to use $aggregate with $project, and to perform a simple $setIntersection in order to get matching ObjectIds.
This is the projection:
db.getCollection('Chats').aggregate([
{
$project: {
Created: 1,
Messages: 1,
Match: {
$setIntersection: [ "$Messages.ReadBy", [ObjectId("57c96a14870ae36ede4d8085")] ]
},
ReadBy: "$Messages.ReadBy"
}
}
])
The result i am getting from this aggregate, seems to insert "$Messages.ReadBy" into Sub-Array of Array [[ObjectId("....")]] for example.
The Result is that $setIntersection returns null although there is something relevant for me, and the new "ReadBy" field i added for debug shows the problem of sub-array:
{
"_id" : ObjectId("58136ba83bdddd2d3cd4b3fb"),
"Created" : ISODate("2016-10-28T15:15:52.563Z"),
"Messages" : [
{
"Created" : ISODate("2016-10-28T15:15:52.567Z"),
"ReadBy" : [
ObjectId("57c96a14870ae36ede4d8085"),
ObjectId("57c972b6fc8effecde6cf0fa")
],
"Content" : "Hello"
}
],
"Match" : [],
"ReadBy" : [
[
ObjectId("57c96a14870ae36ede4d8085"),
ObjectId("57c972b6fc8effecde6cf0fa")
]
]
}
{
"_id" : ObjectId("5813ac380a45415df8e7fc08"),
"Created" : ISODate("2016-10-28T15:16:52.563Z"),
"Messages" : [
{
"Created" : ISODate("2016-10-28T15:15:52.567Z"),
"ReadBy" : [
ObjectId("57c96a14870ae36ede4d8234"),
ObjectId("57c972b6fc8effecde6cf987")
],
"Content" : "Hello2"
}
],
"Match" : [],
"ReadBy" : [
[
ObjectId("57c96a14870ae36ede4d8234"),
ObjectId("57c972b6fc8effecde6cf987")
]
]
}
Why is "Match" field returns empty array?
And why "ReadBy" wrapping the source field with extra array?
a solution is to use $arrayElemAt to get the sub array.
the new query is
db.getCollection('Chats').aggregate([
{
$project:{
Created:1,
Messages:1,
Match:{
$setIntersection:[
{
$arrayElemAt:[
"$Messages.ReadBy",
0
]
},
[
ObjectId("57c96a14870ae36ede4d8085")
]
]
},
ReadBy:"$Messages.ReadBy"
}
}
])
and the result is now
{
"_id" : ObjectId("58136ba83bdddd2d3cd4b3fb"),
"Created" : ISODate("2016-10-28T15:15:52.563Z"),
"Messages" : [
{
"Created" : ISODate("2016-10-28T15:15:52.567Z"),
"ReadBy" : [
ObjectId("57c96a14870ae36ede4d8085"),
ObjectId("57c972b6fc8effecde6cf0fa")
],
"Content" : "Hello"
}
],
"Match" : [
ObjectId("57c96a14870ae36ede4d8085")
],
"ReadBy" : [
[
ObjectId("57c96a14870ae36ede4d8085"),
ObjectId("57c972b6fc8effecde6cf0fa")
]
]
}
{
"_id" : ObjectId("5813ac380a45415df8e7fc08"),
"Created" : ISODate("2016-10-28T15:16:52.563Z"),
"Messages" : [
{
"Created" : ISODate("2016-10-28T15:15:52.567Z"),
"ReadBy" : [
ObjectId("57c96a14870ae36ede4d8234"),
ObjectId("57c972b6fc8effecde6cf987")
],
"Content" : "Hello2"
}
],
"Match" : [ ],
"ReadBy" : [
[
ObjectId("57c96a14870ae36ede4d8234"),
ObjectId("57c972b6fc8effecde6cf987")
]
]
}

Mongo:count the total number of elements in array depending on condition

I am new to mongoDB . I have aware of some basic queries of mongoDb but not advanced.
My document looks like following :
{
"_id" : ObjectId("5289deaa84aedcc100228259"),
"gender" : "male",
"intrest" : [
{
"userId" : ObjectId("5286294984ae18ac5d19af36"),
"timestamp" : ISODate("2013-11-18T09:32:38.040Z"),
"status" : "Pending"
},
{
"userId" : ObjectId("5286295984ae18ac5d19af37"),
"timestamp" : ISODate("2013-11-18T09:33:17.493Z"),
"status" : "Pending"
}
],
"intrestAccepted" : [ ],
"intrestCancled" : [ ],
"intrestDeclined" : [ ],
"intrestReceived" : [
ObjectId("5286294984ae18ac5d19af36"),
ObjectId("5286295984ae18ac5d19af37")
],
"owner" : ObjectId("5286293284ae18ac5d19af35"),
"postDesc" : "gggg",
"tags" : [
"ggg"
]
}
{
"_id" : ObjectId("5289dea084aedcc100228258"),
"gender" : "female",
"intrest" : [
{
"userId" : ObjectId("5286294984ae18ac5d19af36"),
"timestamp" : ISODate("2013-11-18T09:32:42.934Z"),
"status" : "Pending"
},
{
"userId" : ObjectId("5286295984ae18ac5d19af37"),
"timestamp" : ISODate("2013-11-18T09:33:18.520Z"),
"status" : "Pending"
}
],
"intrestAccepted" : [ ],
"intrestCancled" : [ ],
"intrestDeclined" : [ ],
"intrestReceived" : [
ObjectId("5286294984ae18ac5d19af36"),
ObjectId("5286295984ae18ac5d19af37")
],
"owner" : ObjectId("5286293284ae18ac5d19af35"),
"postDesc" : "asdf",
"tags" : [
"asdf"
]
}
{
"_id" : ObjectId("5289dec984aedcc10022825a"),
"gender" : "male",
"intrest" : [
{
"userId" : ObjectId("5286295984ae18ac5d19af37"),
"timestamp" : ISODate("2013-11-18T09:33:20.996Z"),
"status" : "Pending"
}
],
"intrestAccepted" : [ ],
"intrestCancled" : [ ],
"intrestDeclined" : [ ],
"intrestReceived" : [
ObjectId("5286295984ae18ac5d19af37")
],
"owner" : ObjectId("5286294984ae18ac5d19af36"),
"postDesc" : "fff",
"tags" : [
"fff"
]
}
I want to find totalcount for intrestReceived[] whose owner is ObjectId("5286293284ae18ac5d19af35")
What query am I supposed to write. Please help .
You could try something simple like this:
intrestReceived_length = 0
db.col.find({ _id: ObjectId("5286293284ae18ac5d19af35") }).forEach( function( doc ){
intrestReceived_length = doc.intrestReceived.length;
});
The find will extract only the document that matches the ObjectId you provided and then we use a forEach on the cursor to iterate over all of the (one) results. Within the forEach callback, we just use a JavaScript array.length command to extract the size of the array.
You could also output only the intrestReceived array by providing a projection in your find command:
db.col.find({ _id: ObjectId("5286293284ae18ac5d19af35") }, { "intrestReceived": 1 })...
This will return the same document but it will only contain the _id and intrestReceived attributes.

MongoDB Update $push Cannot apply the positional operator without a corresponding query field containing an array

model:
{
"_id" : "a62107e10f388c90a3eb2d7634357c8b",
"_appid" : [
{
"_id" : "1815aaa7f581c838",
"events" : [
{
"_id" : "_TB_launch",
"boday" : [
{
"VERSIONSCODE" : "17",
"NETWORK" : "cmwap",
"VERSIONSNAME" : "2.4.0",
"IMSI" : "460026319223205",
"PACKAGENAME" : "com.androidbox.astjxmjmmshareMM",
"CHANNELID" : "xmjmm17",
"CHANNELNAME" : "浠..?.M寰.俊?.韩?.?1.x锛.,
"eventid" : "_TB_launch",
"uuid" : "a62107e10f388c90a3eb2d7634357c8b",
"creattime" : "1366300799766",
"ts" : ISODate("2013-04-25T06:28:36.403Z")
}
],
"size" : 1
}
],
"size" : 1
}
],
"size" : 1
}
> db.events.update(
{
"_id":"039e569770cec5ff3811e7410233ed27",
"_appid._id":"e880db04064b03bc534575c7f831a83a",
"_appid.events._id":"_TB_launch"
},
{
"$push":{
"_appid.$.events.$.boday":{"111":"123123"}
}
}
);
Cannot apply the positional operator without a corresponding query field containing an array.
Why?!!
You are trying to reference multiple levels of embedding - you can only have one positional $ operator. You won't be able to do something like this until this feature request has been implemented.
Response Here
The short answer is, "no", but working with nested arrays gets
tricky. Here is an example:
db.foo.save({_id: 1, a1:[{_a1id:1, a2:[{a2id:1, a3:[{a3id:1, a4:"data"}]}]}]})
db.foo.find()
{ "_id" : 1, "a1" : [
{ "_a1id" : 1, "a2" : [
{ "a2id" : 1, "a3" : [
{ "a3id" : 1, "a4" : "data" }
] }
] }
] }
db.foo.update({_id:1}, {$push:{"a1.0.a2.0.a3":{a3id:2, a4:"other data"}}})
db.foo.find()
{ "_id" : 1, "a1" : [
{ "_a1id" : 1, "a2" : [
{ "a2id" : 1, "a3" : [
{ "a3id" : 1, "a4" : "data" },
{ "a3id" : 2, "a4" : "other data" }
] }
] }
] }
If you are unsure where one of your sub-documents lies within an
array, you may use one positional operator, and Mongo will update the
first sub-document which matches. For example:
db.foo.update({_id:1, "a1.a2.a2id":1}, {$push:{"a1.0.a2.$.a3":{a3id:2, a4:"other data"}}})

Aggregate of different subtypes in document of a collection

abstract document in collection md given:
{
vals : [{
uid : string,
val : string|array
}]
}
the following, partially correct aggregation is given:
db.md.aggregate(
{ $unwind : "$vals" },
{ $match : { "vals.uid" : { $in : ["x", "y"] } } },
{
$group : {
_id : { uid : "$vals.uid" },
vals : { $addToSet : "$vals.val" }
}
}
);
that may lead to the following result:
"result" : [
{
"_id" : {
"uid" : "x"
},
"vals" : [
[
"24ad52bc-c414-4349-8f3a-24fd5520428e",
"e29dec2f-57d2-43dc-818a-1a6a9ec1cc64"
],
[
"5879b7a4-b564-433e-9a3e-49998dd60b67",
"24ad52bc-c414-4349-8f3a-24fd5520428e"
]
]
},
{
"_id" : {
"uid" : "y"
},
"vals" : [
"0da5fcaa-8d7e-428b-8a84-77c375acea2b",
"1721cc92-c4ee-4a19-9b2f-8247aa53cfe1",
"5ac71a9e-70bd-49d7-a596-d317b17e4491"
]
}
]
as x is the result aggregated on documents containing an array rather than a string, the vals in the result is an array of arrays. what i look for in this case is to have a flattened array (like the result for y).
for me it seems like that what i want to achieve by one aggegration call only, is currently not supported by any given operation as e.g. a type conversion cannot be done or unwind expectes in every case an array as input type.
is map reduce the only option i have? if not ... any hints?
thanks!
You can use the aggregation to do the computation you want without changing your schema (though you might consider changing your schema simply to make queries and aggregations of this field easier to write).
I broke up the pipeline into multiple steps for readability. I also simplified your document slightly, again for readability.
Sample input:
> db.md.find().pretty()
{
"_id" : ObjectId("512f65c6a31a92aae2a214a3"),
"uid" : "x",
"val" : "string"
}
{
"_id" : ObjectId("512f65c6a31a92aae2a214a4"),
"uid" : "x",
"val" : "string"
}
{
"_id" : ObjectId("512f65c6a31a92aae2a214a5"),
"uid" : "y",
"val" : "string2"
}
{
"_id" : ObjectId("512f65e8a31a92aae2a214a6"),
"uid" : "y",
"val" : [
"string3",
"string4"
]
}
{
"_id" : ObjectId("512f65e8a31a92aae2a214a7"),
"uid" : "z",
"val" : [
"string"
]
}
{
"_id" : ObjectId("512f65e8a31a92aae2a214a8"),
"uid" : "y",
"val" : [
"string1",
"string2"
]
}
Pipeline stages:
> project1 = {
"$project" : {
"uid" : 1,
"val" : 1,
"isArray" : {
"$cond" : [
{
"$eq" : [
"$val.0",
[ ]
]
},
true,
false
]
}
}
}
> project2 = {
"$project" : {
"uid" : 1,
"valA" : {
"$cond" : [
"$isArray",
"$val",
[
null
]
]
},
"valS" : {
"$cond" : [
"$isArray",
null,
"$val"
]
},
"isArray" : 1
}
}
> unwind = { "$unwind" : "$valA" }
> project3 = {
"$project" : {
"_id" : 0,
"uid" : 1,
"val" : {
"$cond" : [
"$isArray",
"$valA",
"$valS"
]
}
}
}
Final aggregation:
> db.md.aggregate(project1, project2, unwind, project3, group)
{
"result" : [
{
"_id" : "z",
"vals" : [
"string"
]
},
{
"_id" : "y",
"vals" : [
"string1",
"string4",
"string3",
"string2"
]
},
{
"_id" : "x",
"vals" : [
"string"
]
}
],
"ok" : 1
}
If you modify your schema using always "vals.val" field as an array field (even when the record contains only one element) you can do it easily as follows:
db.test_col.insert({
vals : [
{
uid : "uuid1",
val : ["value1"]
},
{
uid : "uuid2",
val : ["value2", "value3"]
}]
});
db.test_col.insert(
{
vals : [{
uid : "uuid2",
val : ["value4", "value5"]
}]
});
Using this approach you only need to use two $unwind operations: one unwinds the "parent" array and the second unwinds every "vals.val" value. So, querying like
db.test_col.aggregate(
{ $unwind : "$vals" },
{ $unwind : "$vals.val" },
{
$group : {
_id : { uid : "$vals.uid" },
vals : { $addToSet : "$vals.val" }
}
}
);
You can obtain your expected value:
{
"result" : [
{
"_id" : {
"uid" : "uuid2"
},
"vals" : [
"value5",
"value4",
"value3",
"value2"
]
},
{
"_id" : {
"uid" : "uuid1"
},
"vals" : [
"value1"
]
}
],
"ok" : 1
}
And no, you can't execute this query using your current schema, since $unwind fails when the field isn't an array field.