Matching a value in subdocument array and matching array to _id - mongodb

I have a collection of Users which looks like this:
{
"_id": {
"$oid": "5ff6298f2056ad02272d10f2"
},
"name": "Billy Bob",
"story": {
"posts": [
{"id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"},
{"id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"},
{"id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"}
],
"whoCanView": [{
"$oid": "5ff891bd749ed24316272317"
}, {
"$oid": "5ffc89392056ad02272d10f3"
}]
}
}
I want to get all users and their stories listed in an array which I call "contacts". The user currently querying the collection may only get results if their userId is in the "whoCanView" array.
This is what i have so far but doesn't seem to be working:
users.find({'story' : {
'$elementMatch' : {
'_id' : {'$in' : contacts},
ObjectID(userId) : {'$in' : 'story.whoCanView'}
}
}
});
Any guidance would be appreciated. Thanks!

ObjectID(userId) : {'$in' : 'story.whoCanView'}
Above line along with the usage of $elemMatch operator is wrong in your code. Try this query instead:
let contacts = [
ObjectId("5ff6298f2056ad02272d10f2"),
ObjectId("5ff6298f2056ad02272d10f3"),
ObjectId("5ff6298f2056ad02272d10f4")
];
let userId = ObjectId("5ff891bd749ed24316272317");
db.users.find(
{
"_id": { $in: contacts },
"story.whoCanView": userId
},
{
"strories": "$story.posts"
}
);
Output when userId is ObjectId("5ff891bd749ed24316272317"):
/* 1 createdAt:1/7/2021, 2:50:15 AM*/
{
"_id" : ObjectId("5ff6298f2056ad02272d10f2"),
"strories" : [
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
}
]
},
/* 2 createdAt:1/7/2021, 2:50:15 AM*/
{
"_id" : ObjectId("5ff6298f2056ad02272d10f3"),
"strories" : [
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
}
]
},
/* 3 createdAt:1/7/2021, 2:50:15 AM*/
{
"_id" : ObjectId("5ff6298f2056ad02272d10f4"),
"strories" : [
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
}
]
}
Output when userId is ObjectId("5ff891bd749ed24316272318"):
[]
Test data in users collection:
[
{
"_id": ObjectId("5ff6298f2056ad02272d10f2"),
"name": "Billy Bob",
"story": {
"posts": [
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" }
],
"whoCanView": [
ObjectId("5ff891bd749ed24316272317"),
ObjectId("5ffc89392056ad02272d10f3")
]
}
},
{
"_id": ObjectId("5ff6298f2056ad02272d10f3"),
"name": "Billy Bob",
"story": {
"posts": [
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" }
],
"whoCanView": [
ObjectId("5ff891bd749ed24316272317"),
ObjectId("5ffc89392056ad02272d10f3")
]
}
},
{
"_id": ObjectId("5ff6298f2056ad02272d10f4"),
"name": "Billy Bob",
"story": {
"posts": [
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" }
],
"whoCanView": [
ObjectId("5ff891bd749ed24316272317"),
ObjectId("5ffc89392056ad02272d10f3")
]
}
},
{
"_id": ObjectId("5ff6298f2056ad02272d10f5"),
"name": "Billy Bob",
"story": {
"posts": [
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" }
],
"whoCanView": [
ObjectId("5ff891bd749ed24316272317"),
ObjectId("5ffc89392056ad02272d10f3")
]
}
}
]

Related

Filter nested array with conditions based on multi-level object values and update them - MongoDB aggregate + update

Considering I have the following documents in a collection (ignoring the _id) :
[
{
"Id": "OP01",
"Sessions": [
{
"Id": "Session01",
"Conversations": [
{
"Id": "Conversation01",
"Messages": [
{
"Id": "Message01",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message02",
"Status": "delivered",
"Direction": "internal"
},
{
"Id": "Message03",
"Status": "delivered",
"Direction": "inbound"
},
{
"Id": "Message04",
"Status": "sent",
"Direction": "outbound"
}
]
},
{
"Id": "Conversation02",
"Messages": [
{
"Id": "Message05",
"Status": "sent",
"Direction": "outbound"
}
]
}
]
},
{
"Id": "Session02",
"Conversations": [
{
"Id": "Conversation03",
"Messages": [
{
"Id": "Message06",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message07",
"Status": "delivered",
"Direction": "internal"
}
]
},
{
"Id": "Conversation04",
"Messages": []
}
]
}
]
},
{
"Id": "OP02",
"Sessions": [
{
"Id": "Session03",
"Conversations": []
}
]
},
{
"Id": "OP03",
"Sessions": []
}
]
First query — aggregate (+$project)
I want to get the list of Messages grouped by their Conversations where:
Sessions.Id: "Session01"
and
Sessions.Conversations.Messages.Direction $in ["inbound", "outbound"]
and
Sessions.Conversations.Messages.Status $in ["sent", "delivered"]
The expected result is:
[
{
"Id": "Conversation01",
"Messages": [
{
"Id": "Message03",
"Status": "delivered",
"Direction": "inbound"
},
{
"Id": "Message04",
"Status": "sent",
"Direction": "outbound"
}
]
},
{
"Id": "Conversation02",
"Messages": [
{
"Id": "Message05",
"Status": "sent",
"Direction": "outbound"
}
]
}
]
A side note:
If on different documents (or on different Sessions) the Sessions.Id: "Session01" condition is verified ("Session01"is not an unique key), the document's Messages that match the other conditions should also be added.
The result output doesn't mention neither the document or Sessions levels.
Second query — update
I want to update the Sessions.Conversations.Messages.Status of all those messages (same condition as before) to "read".
The collection should have now the following documents:
Please note the changes on:
Sessions.Conversations.Messages.Id = "Message03"
Sessions.Conversations.Messages.Id = "Message04"
Sessions.Conversations.Messages.Id = "Message05"
at Sessions.Id = "Session01"
[
{
"Id": "OP01",
"Sessions": [
{
"Id": "Session01",
"Conversations": [
{
"Id": "Conversation01",
"Messages": [
{
"Id": "Message01",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message02",
"Status": "delivered",
"Direction": "internal"
},
{
"Id": "Message03",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message04",
"Status": "read",
"Direction": "outbound"
}
]
},
{
"Id": "Conversation02",
"Messages": [
{
"Id": "Message05",
"Status": "read",
"Direction": "outbound"
}
]
}
]
},
{
"Id": "Session02",
"Conversations": [
{
"Id": "Conversation03",
"Messages": [
{
"Id": "Message06",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message07",
"Status": "delivered",
"Direction": "internal"
}
]
},
{
"Id": "Conversation04",
"Messages": []
}
]
}
]
},
{
"Id": "OP02",
"Sessions": [
{
"Id": "Session03",
"Conversations": []
}
]
},
{
"Id": "OP03",
"Sessions": []
}
]
How can I accomplish these results with an aggregate and update_one queries?
Here comes a visual explanation of both queries:
I have written the aggregation query
db.session.aggregate([
{
$unwind:"$Sessions"
},
{
$unwind:"$Sessions.Conversations"
},
{
$unwind:"$Sessions.Conversations.Messages"
},
{
$match:{
"Sessions.Id" : "Session01",
"Sessions.Conversations.Messages.Direction":{
$in:[
"inbound", "outbound"
]
},
"Sessions.Conversations.Messages.Status":{
$in:[
"sent", "delivered"
]
}
}
},
{
$group:{
"_id":"$Sessions.Conversations.Id",
"Messages":{
$push:"$Sessions.Conversations.Messages"
}
}
}
]).pretty()
Output
{
"_id" : "Conversation02",
"Messages" : [
{
"Id" : "Message05",
"Status" : "sent",
"Direction" : "outbound"
}
]
}
{
"_id" : "Conversation01",
"Messages" : [
{
"Id" : "Message03",
"Status" : "delivered",
"Direction" : "inbound"
},
{
"Id" : "Message04",
"Status" : "sent",
"Direction" : "outbound"
}
]
}
Now to Update the document:
I have used the positional-filters
db.session.update(
{},
{
$set:{
"Sessions.$[session].Conversations.$[].Messages.$[message].Status":"read"
}
},
{
"arrayFilters": [{"session.Id":"Session01"},{ "message.Id": "Message05" }]
}
)
This will update the status as read for "session.Id":"Session01" and "message.Id": "Message05"
Hope this will help you. :)
UPDATE
db.session.update(
{},
{
$set:{
"Sessions.$[session].Conversations.$[].Messages.$[message].Status":"read"
}
},
{
"arrayFilters": [
{
"session.Id":"Session01"
},
{
"message.Direction": {
$in :[
"inbound",
"outbound"
]
},
"message.Status": {
$in :[
"sent",
"delivered"
]
}
}
]
}
)

Filter by nested arrays/objects values (on different levels) and $project by matching groups (on a specific level) - MongoDB Aggregate

Considering I have the following collection (ignoring documents _id):
Collection
[
{
"Id": "1",
"Level2": [
{
"Id": "1.1",
"Level3": [
{
"Id": "1.1.1",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
]
},
{
"Id": "1.1.2",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "a"
},
{
"Id": "1.1.2.2",
"Status": "c"
}
]
},
{
"Id": "1.1.3",
"Level4": [
{
"Id": "1.1.3.1",
"Status": "c"
}
]
}
]
}
]
},
{
"Id": "2",
"Level2": [
{
"Id": "2.1",
"Level3": [
{
"Id": "2.1.1",
"Level4": [
{
"Id": "2.1.1.1",
"Status": "a"
}
]
}
]
}
]
}
]
Conditions
I want to query it such as:
Level2.Id: "1.1"
Level2.Level3.Level4.Status $in ["a", "b"]
The $project should:
Group Level2.Level3.Level4 matches by Level2.Level3
Should not include Level2.Level3 if it is an empty array
The expected result should be:
Result 1
[
{
"Id": "1.1.1",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
]
},
{
"Id": "1.1.2",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "a"
}
]
}
]
Please notice that if current Level2.Id: "2.1" has the value "1.1" instead, the result should be (matches on different documents):
Result 2
[
{
"Id": "1.1.1",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
]
},
{
"Id": "1.1.2",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "a"
}
]
},
{
"Id": "2.1.1",
"Level4": [
{
"Id": "2.1.1.1",
"Status": "a"
}
]
}
]
How can I accomplish this with an aggregate query?
( filtering different levels and grouping by some level )
Here it is the answer (thank you to #Vijay Rajpurohit for his help on this):
db.collection.aggregate([
{
$unwind: "$Level2"
},
{
$unwind: "$Level2.Level3"
},
{
$unwind: "$Level2.Level3.Level4"
},
{
$match: {
"Level2.Id": "1.1",
"Level2.Level3.Level4.Status": {
$in: [
"a",
"b"
]
}
}
},
{
$group: {
"_id": "$Level2.Level3.Id",
"Messages": {
$push: "$Level2.Level3.Level4"
}
}
}
])
The result of this query is:
[
{
"Messages": [
{
"Id": "1.1.2.1",
"Status": "a"
}
],
"_id": "1.1.2"
},
{
"Messages": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
],
"_id": "1.1.1"
}
]
Mongo Playground

Mongo DB aggregation to convert books to array of authors

My book collection looks like this:
{
"book": {
"title": "Book1",
"author": "Tom"
},
"quantity": 3
},
{
"book": {
"title": "Book2",
"author": "Tom"
},
"quantity": 4
},
{
"book": {
"title": "Book3",
"author": "Dick"
},
"quantity": 9
},
{
"book": {
"title": "Book4",
"author": "Harry"
},
"quantity": 6
},
{
"book": {
"title": "Book5",
"author": "Chris"
},
"quantity": 7
},
{
"book": {
"title": "Book6",
"author": "Dick"
},
"quantity": 10
}
This collection has book documents with title, author and quantity.
I want help in coming up with aggregation which would output the array of authors with their books and quantity. Example - Tom has authored "Book1" and "Book2", "Dick" has authored "Book6" and "Book3".
authors: [
{
"name": "Tom",
"books": [
{
"title": "Book1",
"quantity": "3"
},
{
"title": "Book2",
"quantity": "4"
}
]
},
{
"name": "Dick",
"books": [
{
"title": "Book6",
"quantity": "10"
},
{
"title": "Book3",
"quantity": "9"
}
]
},
{
"name": "Harry",
"books": [
{
"title": "Book4",
"quantity": "6"
}
]
}
]
Try this aggregation:
db.collection.aggregate([{
$group: {
_id: "$book.author",
books: {
$push: {
title: "$book.title",
quantity: "$quantity"
}
}
}},{
$project: {
"name": "$_id",
"books": 1,
"_id": 0
}}])
I tried it on mongo playground:
https://mongoplayground.net/p/m-QbX-uzkkt
Try as below:
db.collection.aggregate([
{
$group: {
_id: "$book.author",
books: { $push: { "title": "$book.title", "quantity": "$quantity"} }
},
}
])
Output will be
/* 1 */
{
"_id" : "Chris",
"books" : [
{
"title" : "Book5",
"quantity" : 7
}
]
},
/* 2 */
{
"_id" : "Harry",
"books" : [
{
"title" : "Book4",
"quantity" : 6
}
]
},
/* 3 */
{
"_id" : "Dick",
"books" : [
{
"title" : "Book3",
"quantity" : 9
},
{
"title" : "Book6",
"quantity" : 10
}
]
},
/* 4 */
{
"_id" : "Tom",
"books" : [
{
"title" : "Book1",
"quantity" : 3
},
{
"title" : "Book2",
"quantity" : 4
}
]
}

Mongo Filter Query Nested with mulitple and

Following is my query does exactly match with my document but still not getting output.Don't know why. Following is the document as well.
db.getCollection("analytics").find(
{
"$and" : [
{
"archive" : false
},
{
"platform" : "WEB"
},
{
"vendorId" : "3c7adbfe-14d7-4b26-9134-7e05d56573cc"
},
{
"createdAt" : {
"$gte" : 1578268800000.0
}
},
{
"createdAt" : {
"$lte" : 1580860800000.0
}
},
{
"$and" : [
{
"data.mobile" : "123"
},
{
"page" : "Loan Application"
},
{
"event" : "click"
}
]
},
{
"$and" : [
{
"data.aadharNumber" : "123"
},
{
"page" : "Personal Information"
},
{
"event" : "click"
}
]
},
{
"$and" : [
{
"data.totalExp" : "5"
},
{
"page" : "Professional Information"
},
{
"event" : "click"
}
]
}
]
}
);
Documents :
[
{
"page": "Loan Application",
"event": "click",
"loggedIn": true,
"vendorId": "3c7adbfe-14d7-4b26-9134-7e05d56573cc",
"data": {
"first": "Praveen",
"mobile": "1234"
},
"platform": "WEB"
},
{
"page": "Personal Information",
"event": "click",
"loggedIn": true,
"vendorId": "3c7adbfe-14d7-4b26-9134-7e05d56573cc",
"data": {
"panNumber": "123",
"aadharNumber": "123"
},
"platform": "WEB"
},
{
"page": "Professional Information",
"event": "click",
"loggedIn": true,
"vendorId": "3c7adbfe-14d7-4b26-9134-7e05d56573cc",
"data": {
"totalExp": "5"
},
"platform": "WEB"
}
]
There are a lot of issues going on with your query, you can try below query to return all documents :
db.getCollection("analytics").find({
$expr: {
$and: [
{
$eq: [
"$platform",
"WEB"
]
},
{
$eq: [
"$vendorId",
"3c7adbfe-14d7-4b26-9134-7e05d56573cc"
]
},
{
$or: [
{
"$and": [
{
"data": {
"mobile": "123"
}
},
{
"page": "Loan Application"
},
{
"event": "click"
}
]
},
{
"$and": [
{
"data": {
"aadharNumber": "123"
}
},
{
"page": "Personal Information"
},
{
"event": "click"
}
]
},
{
"$and": [
{
"data": {
"totalExp": "5"
}
},
{
"page": "Professional Information"
},
{
"event": "click"
}
]
}
]
}
]
}
})
Test : MongoDB-Playground

Mongodb query select embedded document

I have following Mongodb document. Would like to fetch document where participant = 'xxx' and (message.touserid = 'xxx' or message.fromuserid = 'xxx').
I am using following query but it is returning all messages instead of just one. Could you please let me know how to achieve this result?
{ "$and" : [ { "participants" : { "$regex" : "56d314a8e4b04d7f98cfd0c6"} , "$or" : [ { "messages.touserId" : "56d314a8e4b04d7f98cfd0c6"} , { "messages.formuserId" : "56d314a8e4b04d7f98cfd0c6"}]}]} fields: { "_id" : "0" , "product" : "0" , "participants" : "0" , "messages" : "0"}
{
"_id": {
"$oid": "574eb878027520c2158268d6"
},
"_class": "com.idearealty.product.shopchat.persistence.model.Discussion",
"participants": "56d314a8e4b04d7f98cfd0c6,56d5d48ee4b0cc330f512a47,56d9d599e4b0cc330f512aaa,57130299e4b08c554c1092c7,56841002eceefce22f455c7f",
"messages": [
{
"_id": {
"$oid": "574eb874027520c2158268d2"
},
"formuserId": "56841002eceefce22f455c7f",
"fromuser": "9674642375",
"touserId": "56d314a8e4b04d7f98cfd0c6",
"touser": "debopam_r",
"message": "Creating Discussion",
"isMute": false,
"index": 1,
"createDate": {
"$date": "2016-06-01T10:27:00.500Z"
},
"lastModifiedDate": {
"$date": "2016-06-01T10:27:00.501Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
},
{
"_id": {
"$oid": "574eb875027520c2158268d3"
},
"formuserId": "56841002eceefce22f455c7f",
"fromuser": "9674642375",
"touserId": "56d5d48ee4b0cc330f512a47",
"touser": "Raushan",
"message": "Creating Discussion",
"isMute": false,
"index": 2,
"createDate": {
"$date": "2016-06-01T10:27:01.295Z"
},
"lastModifiedDate": {
"$date": "2016-06-01T10:27:01.295Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
},
{
"_id": {
"$oid": "574eb875027520c2158268d4"
},
"formuserId": "56841002eceefce22f455c7f",
"fromuser": "9674642375",
"touserId": "56d9d599e4b0cc330f512aaa",
"touser": "anirbanshop1",
"message": "Creating Discussion",
"isMute": false,
"index": 3,
"createDate": {
"$date": "2016-06-01T10:27:01.962Z"
},
"lastModifiedDate": {
"$date": "2016-06-01T10:27:01.962Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
},
{
"_id": {
"$oid": "574eb876027520c2158268d5"
},
"formuserId": "56841002eceefce22f455c7f",
"fromuser": "9674642375",
"touserId": "57130299e4b08c554c1092c7",
"touser": "dummyshop",
"message": "Creating Discussion",
"isMute": false,
"index": 4,
"createDate": {
"$date": "2016-06-01T10:27:02.574Z"
},
"lastModifiedDate": {
"$date": "2016-06-01T10:27:02.574Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
}
],
"messageCount": 4,
"createDate": {
"$date": "2016-06-01T10:27:04.041Z"
},
"lastModifiedDate": {
"$date": "2016-06-01T10:27:04.041Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
}
As this is a complex match on elements - $elemMatch cannot be used in this case,
so aggregation framework is a helper.
var match = {
$match : {
participants : /56d314a8e4b04d7f98cfd0c6/
}
}
var unwind = {
$unwind : "$messages"
}
var matchSecond = {
$match : {
$or : [{
"messages.touserId" : "56d314a8e4b04d7f98cfd0c6"
}, {
"messages.formuserId" : "56d314a8e4b04d7f98cfd0c6"
}
]
}
}
var projection = {
$project : {
_id : 0,
messages : 1
}
}
db.deb.aggregate([match, unwind, matchSecond, projection])
and output:
{
"messages" : {
"_id" : {
"oid" : "574eb874027520c2158268d2"
},
"formuserId" : "56841002eceefce22f455c7f",
"fromuser" : "9674642375",
"touserId" : "56d314a8e4b04d7f98cfd0c6",
"touser" : "debopam_r",
"message" : "Creating Discussion",
"isMute" : false,
"index" : 1.0,
"createDate" : {
"date" : "2016-06-01T10:27:00.500Z"
},
"lastModifiedDate" : {
"date" : "2016-06-01T10:27:00.501Z"
},
"createdBy" : "9674642375",
"lastModifiedBy" : "9674642375"
}
}