MongoDB: How to make two unions to the same collection with matrices - mongodb

I have user records with posts and posts shared with them, that is, users can share the posts with other users. I need to be able to bring or get only the posts shared with him, using as a reference the id of the user who shared the post and the id of the post.
when I use the user id as a reference, it works, but when I try to combine it with the id of the post it does not get anything, this happens when I try to use only the id of the post to get the shared posts.
This would be the structure of the records
{
"_id" : ObjectId("5cd573b2bb9ad84f9bba2f74"),
"name" : "name 4",
"posts" : [
{
"_id" : ObjectId("5cd573b2bb9ad84f9bba2f72"),
"name" : "post 1"
},
{
"_id" : ObjectId("5cd573b2bb9ad84f9bba2f73"),
"name" : "post 2"
}
],
"postSharedWithMe" : [
{
"user_id" : "5cd4aaedfcf8d8583cf97494",
"post_id" : "5cd4aaedfcf8d8583cf97492"
},
{
"user_id" : "5cd4aaedfcf8d8583cf97494",
"post_id" : "5cd4aaedfcf8d8583cf97493"
}
]
}
and in this way he tried to consult them
db.users.aggregate([
{ "$match": { "_id": ObjectId("5cd573b2bb9ad84f9bba2f74") }},
{ $unwind:"$postSharedWithMe" },
{ $unwind:"$posts" },
{
$lookup:
{
from: "users",
let: {
user_id: { "$toObjectId": "$postSharedWithMe.user_id"},
post_id : { "$toObjectId": "$postSharedWithMe.post_id"}
},
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$_id", "$$user_id" ] },
{ $eq: [ "$posts._id", "$$post_id" ] }
]
}
}
},
],
as: "sharedPosts"
}
},
{ $unwind:"$sharedPosts" },
{ "$group": {
"_id": "$_id",
"sharedPosts": { "$push": "$sharedPosts" }
}
}
])
and this is the result:
Fetched 0 record(s) in 0ms
and this is what I expected
{
"_id" : ObjectId("5cd573b2bb9ad84f9bba2f74"),
"name" : "username",
"posts" : [
{
"_id" : ObjectId("5cd573b2bb9ad84f9bba2f72"),
"name" : "post 1"
},
{
"_id" : ObjectId("5cd573b2bb9ad84f9bba2f73"),
"name" : "post 2"
}
],
"sharedPosts" : [
{
"_id" : ObjectId("id"),
"name" : "shared post"
},
{
"_id" : ObjectId("id"),
"name" : "shared post"
}
]
}

apparently I needed to go through all the posts first while referring to the shared posts, the result of this was an array, now I just needed to make the $ unwind and compare with $ eq and it worked!
db.users.aggregate([
{ $match: { "_id": ObjectId("5cd573b2bb9ad84f9bba2f74") }},
{ $unwind: "$postSharedWithMe" },
{
$lookup:
{
from: "users",
let: {
user_id: { $toObjectId: "$postSharedWithMe.user_id"},
post_id : { $toObjectId: "$postSharedWithMe.post_id"}
},
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$$user_id", "$_id" ] },
{ $in: ["$$post_id", "$posts._id" ] },
]
}
}
},
{ $unwind: "$posts" },
{ $match: { $expr: { $eq: [ "$posts._id", "$$post_id" ] } } },
],
as: "sharedPosts"
}
},
{ $unwind: "$sharedPosts" },
{ $group: {
_id: "$_id",
name: { "$first": "$name" },
posts: { "$first": "$posts" },
sharedPosts: { $push:
"$sharedPosts.posts"
}
}
}
])

Related

Query nested array from document

Given the following document data in collection called 'blah'...
[
{
"_id" : ObjectId("60913f55987438922d5f0db6"),
"procedureCode" : "code1",
"description" : "Description 1",
"coding" : [
{
"system" : "ABC",
"code" : "L111"
},
{
"system" : "DEFG",
"code" : "S222"
}
]
},
{
"_id" : ObjectId("60913f55987438922d5f0dbc"),
"procedureCode" : "code2",
"description" : "Description 2",
"coding" : [
{
"system" : "ABC",
"code" : "L999"
},
{
"system" : "DEFG",
"code" : "X3333"
}
]
}
]
What I want to get is all of the coding elements where system is ABC for all parents, and an array of codes like so.
[
{ "code": "L111" },
{ "code": "L999" },
]
If I use db.getCollection('blah').find({"coding.system": "ABC"}) I get the parent document with any child in the coding array of ICD.
If I use...
db.getCollection("blah")
.find({ "coding.system": "ABC" })
.projection({ "coding.code": 1 })
I do get the parent documents which have a child with a system of "ABC", but the coding for "DEFG" seems to come along for the ride too.
{
"_id" : ObjectId("60913f55987438922d5f0db6"),
"coding" : [
{
"code" : "L989"
},
{
"code" : "S102"
}
]
},
{
"_id" : ObjectId("60913f55987438922d5f0dbc"),
"coding" : [
{
"code" : "L989"
},
{
"code" : "X382"
}
]
}
I have also tried experimenting with:
db.getCollection("blah").aggregate(
{ $unwind: "$coding" },
{ $match: { "system": "ICD" } }
);
.. as per this page: mongoDB query to find the document in nested array
... but go no where fast with that approach. i.e. no records at all.
What query do I need, please, to achieve something like this..?
[
{ "code": "L111" },
{ "code": "L999" },
...
]
or even better, this..?
[
"L111",
"L999",
...
]
db.collection.aggregate([
{
$match: { "coding.system": "ABC" }
},
{
$unwind: "$coding"
},
{
$match: { "coding.system": "ABC" }
},
{
$project: { code: "$coding.code" }
}
])
mongoplayground
db.collection.aggregate([
{
$match: { "coding.system": "ABC" }
},
{
$unwind: "$coding"
},
{
$match: { "coding.system": "ABC" }
},
{
$group: {
_id: null,
coding: { $push: "$coding.code" }
}
}
])
mongoplayground
Instead of $unwind, $match you can also use $filter:
db.collection.aggregate([
{ $match: { "coding.system": "ABC" } },
{
$project: {
coding: {
$filter: {
input: "$coding",
cond: { $eq: [ "$$this.system", "ABC" ] }
}
}
}
}
])

How to avoid possible null error scenarios in mongodb Aggregate

I've set up a fairly long mongo aggregate query to join several mongo collections together and shape up them into output of set of string fields. The query works fine as long as all the required values (ie : ids) exists but it breaks when it encounters null or empty values when doing the $lookup.
Following is the patientFile collection thats being queried :
{
"no" : "2020921008981",
"startDateTime" : ISODate("2020-04-01T05:19:02.263+0000")
"saleId" : "5e8424464475140d19c6941b",
"patientId" : "5e8424464475140d1955941b"
}
sale collection :
{
"_id" : ObjectId("5e8424464475140d19c6941b"),
"invoices" : [
{
"billNumber" : "2020921053467",
"type" : "CREDIT",
"insurancePlanId" : "160"
},
{
"billNumber" : "2020921053469",
"type" : "DEBIT",
"insurancePlanId" : "161"
}
],
"status" : "COMPLETE"
}
insurance collection :
{
"_id" : ObjectId("5b55aca20550de00210a6d25"),
"name" : "HIJKL"
"plans" : [
{
"_id" : "160",
"name" : "UVWZ",
},
{
"_id" : "161",
"name" : "LMNO",
}
]
}
patient collection :
{
"_id" : ObjectId("5b55cc5c0550de00217ae0f3"),
"name" : "TAN NAI",
"userId" : {
"number" : "787333128H"
}
}
Heres the aggregate query :
db.getCollection("patientFile").aggregate([
{ $match: { "startDateTime": { $gte: ISODate("2020-01-01T00:00:00.000Z"),
$lt: ISODate("2020-05-01T00:00:00.000Z") } } },
{
$lookup:
{
from: "patient",
let: { pid: "$patientId" },
pipeline: [
{
$match: {
$expr: {
$eq: ["$_id", { $toObjectId: "$$pid" }]
}
}
},
{ "$project": { "name": 1, "userId.number": 1, "_id": 0 } }
],
as: "patient"
}
},
{
$lookup:
{
from: "sale",
let: { sid: "$saleId" },
pipeline: [
{
$match: {
$expr: {
$eq: ["$_id", { $toObjectId: "$$sid" }]
}
}
}
],
as: "sale"
}
},
{ $unwind: "$sale" },
{ $unwind: "$patient" },
{
$lookup: {
from: "insurance",
let: { pid: {$ifNull:["$sale.bill.insurancePlanId", [] ]} },
pipeline: [
{
$unwind: "$plans"
},
{
$match: { $expr: { $in: ["$plans._id", "$$pid"] } }
},
{
$project: { _id: 0, name: 1 }
}
],
as: "insurances"
}
},
{ $match: { "insurances.name": { $exists: true, $ne: null } } },
{
$addFields: {
invoice: {
$reduce: {
input: {$ifNull:["$sale.bill.billNumber", [] ]},
initialValue: "",
in: {
$cond: [{ "$eq": ["$$value", ""] }, "$$this", { $concat: ["$$value", "\n", "$$this"] }]
}
}
},
insurances: {
$reduce: {
input: {$ifNull:["$insurances.name", [] ]},
initialValue: "",
in: {
$cond: [{ "$eq": ["$$value", ""] }, "$$this", { $concat: ["$$value", "\n", "$$this"] }]
}
}
}
}
},
{
"$project": {
"startDateTime": 1,
"patientName": "$patient.name",
"invoice": 1,
"insurances": 1
}
}
],
{ allowDiskUse: true }
)
Error :
Unable to execute the selected commands
Mongo Server error (MongoCommandException): Command failed with error 241 (ConversionFailure): 'Failed to parse objectId '' in $convert with no onError value: Invalid string length for parsing to OID, expected 24 but found 0' on server localhost:27017.
The full response is:
{
"ok" : 0.0,
"errmsg" : "Failed to parse objectId '' in $convert with no onError value: Invalid string length for parsing to OID, expected 24 but found 0",
"code" : NumberInt(241),
"codeName" : "ConversionFailure"
}
As a solution i have found, used $ifNull but this error keeps coming. What would be the best step to take for this scenario?
I see a couple of ways:
Instead of converting the string value to an ObjectId to test, convert the ObjectId to a string
$match: {
$expr: {
$eq: [{$toString: "$_id"}, "$$pid" ]
}
}
Instead of the $toObjectId helper, use $convert and provide onError and/or onNull values:
$match: {
$expr: {
$eq: ["$_id", { $convert: {
input: "$$pid",
to: "objectId",
onError: {error:true},
onNull: {isnull:true}
}}]
}
}

$unwind, $aggregation manipulation in mongodb nodejs

please check this query
db.billsummaryofthedays.aggregate([
{
'$match': {
'userId': ObjectId('5e43de778b57693cd46859eb'),
'adminId': ObjectId('5e43e5cdc11f750864f46820'),
'date': ISODate("2020-02-11T16:30:00Z"),
}
},
{
$lookup:
{
from: "paymentreceivables",
let: { userId: '$userId', adminId: '$adminId' },
pipeline: [
{
$match:
{
paymentReceivedOnDate:ISODate("2020-02-11T16:30:00Z"),
$expr:
{
$and:
[
{ $eq: ["$userId", "$$userId"] },
{ $eq: ["$adminId", "$$adminId"] }
]
}
}
},
{ $project: { amount: 1, _id: 0 } }
],
as: "totalPayment"
}
}, {'$unwind':'$totalPayment'},
{ $group:
{ _id:
{ date: '$date',
userId: '$userId',
adminId: '$adminId' },
totalBill:
{
$sum: '$billOfTheDay'
},
totalPayment:
{
$sum: '$totalPayment.amount'
}
}
},
}
}])
this is the result i am getting in the shell
{
"_id" : {
"date" : ISODate("2020-02-11T18:30:00Z"),
"userId" : ObjectId("5e43de778b57693cd46859eb"),
"adminId" : ObjectId("5e43e5cdc11f750864f46820")
},
"totalBill" : 1595.6799999999998,
"totalPayments" : 100
}
now this is not what i expected,i assume due to {'$unwind':'$totalPayment'} it takes out all the values from the array and because of which every document is getting counted 2 times. When i remove {'$unwind':'$totalPayment'} then totalBill sum turns out to be correct but totalPayment is 0.
I have tried several other ways but not able to achieve the desired result
Below are my collections:
// collection:billsummaryofthedays//
{
"_id" : ObjectId("5e54f784f4032c1694535c0e"),
"userId" : ObjectId("5e43de778b57693cd46859eb"),
"adminId" : ObjectId("5e43e5cdc11f750864f46820"),
"date" : ISODate("2020-02-11T16:30:00Z"),
"UID":"acex01"
"billOfTheDay" : 468,
}
{
"_id" : ObjectId("5e54f784f4032c1694535c0f"),
"UID":"bdex02"
"userId" : ObjectId("5e43de778b57693cd46859eb"),
"adminId" : ObjectId("5e43e5cdc11f750864f46820"),
"date" : ISODate("2020-02-11T16:30:00Z"),
"billOfTheDay" : 329.84,
}
// collection:paymentreceivables//
{
"_id" : ObjectId("5e43e73169fe1e3fc07eb7c5"),
"paymentReceivedOnDate" : ISODate("2020-02-11T16:30:00Z"),
"adminId" : ObjectId("5e43e5cdc11f750864f46820"),
"userId" : ObjectId("5e43de778b57693cd46859eb"),
"amount" : 20,
}
{
"_id" : ObjectId("5e43e73b69fe1e3fc07eb7c6"),
"paymentReceivedOnDate" : ISODate("2020-02-11T16:30:00Z"),
"adminId" : ObjectId("5e43e5cdc11f750864f46820"),
"userId" : ObjectId("5e43de778b57693cd46859eb"),
"amount" : 30,
}
desired result should be totalBill:797.83 i.e[468+329.84,] and totalPayment:50 i.e[30+20,] but i am getting double the expected result and even if i am able to calculate one of the value correctly the other one result 0.How to tackle this??
Since you've multiple documents with same data in billsummaryofthedays collection then you can group first & then do $lookup - that way JOIN between two collections would be 1-Vs-many rather than many-Vs-many as like it's currently written, So you can try below query for desired o/p & performance gains :
db.billsummaryofthedays.aggregate([
{
"$match": {
"userId": ObjectId("5e43de778b57693cd46859eb"),
"adminId": ObjectId("5e43e5cdc11f750864f46820"),
"date": ISODate("2020-02-11T16:30:00Z"),
}
},
{
$group: {
_id: {
date: "$date",
userId: "$userId",
adminId: "$adminId"
},
totalBill: {
$sum: "$billOfTheDay"
}
}
},
{
$lookup: {
from: "paymentreceivables",
let: {
userId: "$_id.userId",
adminId: "$_id.adminId"
},
pipeline: [
{
$match: {
paymentReceivedOnDate: ISODate("2020-02-11T16:30:00Z"),
$expr: {
$and: [
{
$eq: [
"$userId",
"$$userId"
]
},
{
$eq: [
"$adminId",
"$$adminId"
]
}
]
}
}
},
{
$project: {
amount: 1,
_id: 0
}
}
],
as: "totalPayment"
}
},
{
$addFields: {
totalPayment: {
$reduce: {
input: "$totalPayment",
initialValue: 0,
in: {
$add: [
"$$value",
"$$this.amount"
]
}
}
}
}
}
])
Test : MongoDB-Playground

How to do $lookup in an array's field and add the foreign table's content in the same query?

I am trying to make a query in which I have a db_task query that contains the task id and the users assigned to it. I have to fetch the user details which are present in db_user collection and add the incoming details into the same document.
db_task
{
"_id" : ObjectId("5d8b522d0cf2579c57bc8ce0"),
"users" : [
{
"user_id" : ObjectId("5d8b522d0cf2579e27bc8ce3"),
"is_finished" : false
},
{
"user_id" : ObjectId("5d6f6d25e079b9fb7d858236"),
"is_finished" : false
}
]
}
The users field the users who are assigned that task, I want to do a lookup on the db_user query which would get me the details inside the same embedded document.
db_user
{
"_id" : ObjectId("5d8b522d0cf2579e27bc8ce3"),
"first_name" : "Harry",
"last_name" : "Paul"
},
{
"_id" : ObjectId("5d6f6d25e079b9fb7d858236"),
"first_name" : "Aaron",
"last_name" : "Potter"
}
I tried to do $lookup on the db_user table with "users.user_id" but that would fetch me a new field and then I tried to concatenate those arrays with "$concatArrays" but the result still wasn't what I expected.
I want to get the output in a format something like this
db_task
{
"_id" : ObjectId("5d8b522d0cf2579c57bc8ce0"),
"users" : [
{
"user_id" : ObjectId("5d8b522d0cf2579e27bc8ce3"),
"is_finished" : false,
"user_info":{
"first_name" : "Harry",
"last_name" : "Paul"
}
},
{
"user_id" : ObjectId("5d6f6d25e079b9fb7d858236"),
"is_finished" : false,
"user_info":{
"first_name" : "Aaron",
"last_name" : "Potter"
}
}
]
}
Altough they're working, the provided solutions, with unwind and group, can be expensive in resources.
Here's a better solution in only two stages :
db.db_task.aggregate([
{
$lookup: {
from: "db_user",
localField: "users.user_id",
foreignField: "_id",
as: "usersInfos"
}
},
{
$project: {
users: {
$map: {
input: "$usersInfos",
as: "ui",
in: {
$mergeObjects: [
"$$ui",
{
$arrayElemAt: [
{
$filter: {
input: "$users",
as: "users",
cond: {
$eq: [
"$$users.user_id",
"$$ui._id"
]
}
}
},
0
]
}
]
}
}
}
}
}
])
Will output
[
{
"_id": ObjectId("5d8b522d0cf2579c57bc8ce0"),
"users": [
{
"_id": ObjectId("5d6f6d25e079b9fb7d858236"),
"first_name": "Aaron",
"is_finished": false,
"last_name": "Potter",
"user_id": ObjectId("5d6f6d25e079b9fb7d858236")
},
{
"_id": ObjectId("5d8b522d0cf2579e27bc8ce3"),
"first_name": "Harry",
"is_finished": true,
"last_name": "Paul",
"user_id": ObjectId("5d8b522d0cf2579e27bc8ce3")
}
]
}
]
Note : as proposed by #Valijon, you can add a $project stage if you need to slighty re-arrange from here.
It's Work For You.
db_task.aggregate([
{
$match: { '_id': ObjectId("5d8b522d0cf2579c57bc8ce0") }
},
{
$unwind: '$users'
},
{
$lookup: {
from: 'db_user',
localField: 'users.user_id',
foreignField: '_id',
as: 'user_info'
}
},
{
$project: {
$users: {
user_id: 1,
is_finished: 1,
user_info:'$user_info',
}
}
},
{
$group: {
_id: '$_id',
users: {
$push: '$users'
},
}
},
])
Almost the same as vishal pankhaniya solution, but we exclude user_info._id from inner document
db.db_task.aggregate([
{
$unwind: "$users"
},
{
$lookup: {
from: "db_user",
localField: "users.user_id",
foreignField: "_id",
as: "user_info"
}
},
{
$project: {
users: {
user_id: 1,
is_finished: 1,
user_info: "$user_info"
}
}
},
{
$group: {
_id: "$_id",
users: {
$push: "$users"
}
}
},
{
$project: {
"users.user_info._id": 0
}
}
])
Result
[
{
"_id": ObjectId("5d8b522d0cf2579c57bc8ce0"),
"users": [
{
"is_finished": false,
"user_id": ObjectId("5d8b522d0cf2579e27bc8ce3"),
"user_info": [
{
"first_name": "Harry",
"last_name": "Paul"
}
]
},
{
"is_finished": false,
"user_id": ObjectId("5d6f6d25e079b9fb7d858236"),
"user_info": [
{
"first_name": "Aaron",
"last_name": "Potter"
}
]
}
]
}
]
MongoPlayground

MongoDB query with conditional group by statement

I need to export customer records from database of mongoDB. Exported customer records should not have duplicated values. "firstName+lastName+code" is the key to DE-duped the record and If there are two records present in database with same key then I need to give preference to source field with value other than email.
customer (id,firstName,lastName,code,source) collection is this.
If there are record 3 records with same unique key and 3 different sources then i need to choose only one record between 2 sources(TV,internet){or if there are n number of sources i need the one record only}not with the 'email'(as email will be choosen when only one record is present with the unique key and source is email)
query using:
db.customer.aggregate([
{
"$match": {
"active": true,
"dealerCode": { "$in": ["111391"] },
"source": { "$in": ["email", "TV", "internet"] }
}
},
{
$group: {
"_id": {
"firstName": "$personalInfo.firstName",
"lastName": "$personalInfo.lastName",
"code": "$vehicle.code"
},
"source": {
$addToSet: { "source": "$source" }
}
}
},
{
$redact:
{
$cond: [
{ $eq: [{ $ifNull: ["$source", "other"] }, "email"] },
"$$PRUNE",
"$$DESCEND"
]
}
},
{
$project:
{
"source":
{
$map:
{
"input": {
$cond: [
{ $eq: [{ $size: "$source" }, 0] },
[{ "source": "email" }],
"$source"
]
},
"as": "inp",
"in": "$$inp.source"
}
},
"record": { "_id": 1 }
}
}
])
sample output:
{ "_id" : { "firstName" : "sGI6YaJ36WRfI4xuJQzI7A==", "lastName" : "99eQ7i+uTOqO8X+IPW+NOA==", "code" : "1GTHK23688F113955" }, "source" : ["internet"] }
{ "_id" : { "firstName" : "WYDROTF/9vs9O7XhdIKd5Q==", "lastName" : "BM18Uq/ltcbdx0UJOXh7Sw==", "code" : "1G4GE5GV5AF180133" }, "source" : ["internet"] }
{ "_id" : { "firstName" : "id+U2gYNHQaNQRWXpe34MA==", "lastName" : "AIs1G33QnH9RB0nupJEvjw==", "code" : "1G4GE5EV0AF177966" }, "source" : ["internet"] }
{ "_id" : { "firstName" : "qhreJVuUA5l8lnBPVhMAdw==", "lastName" : "petb0Qx3YPfebSioY0wL9w==", "code" : "1G1AL55F277253143" }, "source" : ["TV"] }
{ "_id" : { "firstName" : "qhreJVuUA5l8lnBPVhMAdw==", "lastName" : "6LB/NmhbfqTagbOnHFGoog==", "code" : "1GCVKREC0EZ168134" }, "source" : ["TV", "internet"] }
This is a problem with this query please suggest :(
Your code doesn't work, because $cond is not an accumulator operator. Only these accumulator operators, can be used in a $group stage.
Assuming your records contain not more than two possible values of source as you mention in your question, you could add a conditional $project stage and modify the $group stage as,
Code:
db.customer.aggregate([
{
$group: {
"_id": {
"id": "$id",
"firstName": "$firstName",
"lastName": "$lastName",
"code": "$code"
},
"sourceA": { $first: "$source" },
"sourceB": { $last: "$source" }
}
},
{
$project: {
"source": {
$cond: [
{ $eq: ["$sourceA", "email"] },
"$sourceB",
"$sourceA"
]
}
}
}
])
In case there can be more that two possible values for source, then you could do the following:
Group by the id, firstName, lastName and code. Accumulate
the unique values of source, using the $addToSet operator.
Use $redact to keep only the values other than email.
Project the required fields, if the source array is empty(all the elements have been removed), add a
value email to it.
Unwind the source field to list it as a field and not an array.
(optional)
Code:
db.customer.aggregate([
{
$group: {
"_id": {
"id": "$id",
"firstName": "$firstName",
"lastName": "$lastName",
"code": "$code"
},
"sourceArr": { $addToSet: { "source": "$source" } }
}
},
{
$redact: {
$cond: [
{ $eq: [{ $ifNull: ["$source", "other"] }, "email"] },
"$$PRUNE",
"$$DESCEND"
]
}
},
{
$project: {
"source": {
$map: {
"input":
{
$cond: [
{ $eq: [{ $size: "$sourceArr" }, 0] },
[{ "source": "item" }],
"$sourceArr"]
},
"as": "inp",
"in": "$$inp.source"
}
}
}
}
])