I am stuck on an issue related to $in in MongoDB, not getting the desired results.
Here is my MongoDB collection data for better visualisation
[
{
"_id" : ObjectId("5aeaf7c73c3e9de82d91e439"),
"companyID" : "4",
"accounts" : [
{
"_id" : ObjectId("5aeaf7c720262a1db759edf5"),
"userID" : "1",
"preferences" : [
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeaf7c720262a1db759edf7"),
"preferenceID" : "6fbd6c-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T11:51:35.509Z"),
"updatedAt" : ISODate("2018-05-03T11:51:35.509Z")
},
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeaf7c720262a1db759edf6"),
"preferenceID" : "6fb118-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T11:51:35.509Z"),
"updatedAt" : ISODate("2018-05-03T11:51:35.509Z")
}
]
}
],
"__v" : 0
},
{
"_id" : ObjectId("5aeafe693c3e9de82d91e43a"),
"companyID" : "5",
"accounts" : [
{
"_id" : ObjectId("5aeafe698b1d5f2057419c99"),
"userID" : "1",
"preferences" : [
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeafe698b1d5f2057419c9b"),
"preferenceID" : "6fbd6c-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T12:19:53.436Z"),
"updatedAt" : ISODate("2018-05-03T12:19:53.436Z")
},
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeafe698b1d5f2057419c9a"),
"preferenceID" : "6fb118-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T12:19:53.436Z"),
"updatedAt" : ISODate("2018-05-03T12:19:53.436Z")
}
]
}
],
"__v" : 0
},
{
"_id" : ObjectId("5aeafe6d3c3e9de82d91e43b"),
"companyID" : "6",
"accounts" : [
{
"_id" : ObjectId("5aeafe6d8b1d5f2057419c9c"),
"userID" : "1",
"preferences" : [
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeafe6d8b1d5f2057419c9e"),
"preferenceID" : "6fbd6c-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T12:19:57.331Z"),
"updatedAt" : ISODate("2018-05-03T12:19:57.331Z")
},
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeafe6d8b1d5f2057419c9d"),
"preferenceID" : "6fb118-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T12:19:57.331Z"),
"updatedAt" : ISODate("2018-05-03T12:19:57.331Z")
}
]
},
{
"_id" : ObjectId("5aeafe738b1d5f2057419c9f"),
"userID" : "2",
"preferences" : [
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeafe738b1d5f2057419ca1"),
"preferenceID" : "6fbd6c-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T12:20:03.987Z"),
"updatedAt" : ISODate("2018-05-03T12:20:03.987Z")
},
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeafe738b1d5f2057419ca0"),
"preferenceID" : "6fb118-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T12:20:03.987Z"),
"updatedAt" : ISODate("2018-05-03T12:20:03.987Z")
}
]
},
{
"_id" : ObjectId("5aeafe778b1d5f2057419ca2"),
"userID" : "3",
"preferences" : [
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeafe778b1d5f2057419ca4"),
"preferenceID" : "6fbd6c-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T12:20:07.062Z"),
"updatedAt" : ISODate("2018-05-03T12:20:07.062Z")
},
{
"emailNotification" : true,
"smsNotification" : true,
"pushNotification" : false,
"webNotification" : false,
"lastUpdatedBy" : "SYSTEM",
"_id" : ObjectId("5aeafe778b1d5f2057419ca3"),
"preferenceID" : "6fb118-4c56-11e8-842f-0ed5f89f718b",
"createdAt" : ISODate("2018-05-03T12:20:07.062Z"),
"updatedAt" : ISODate("2018-05-03T12:20:07.062Z")
}
]
}
],
"__v" : 0
}]
What I am trying to get from the data. I am trying fetch each company's user preferences based on a companyID & userID, but for multiple company and multiple users, not for a single one.
Say for an example I have this input Data [userID:2,companyID: 6] and [userID:1, companyID:4]
The output based on above added documents data, should be
[
{
"userID":2,
"companyID":6,
"preferences":[] // the preferences from that db
},
{
"userID":1,
"companyID":4,
"preferences":[] // their respective preferences array
}
]
The above is the desired output, Now my approach for solving this
Approach
I am getting input parameters in the form array of objects liks this
input_json = [{ "userID":2, "companyID":6 }, { "userID":1, "companyID":4 }, ]After getting the input JSON, I am making two different arrays users array, company's array,
user_arraywill hold all the userIDs,company_arraywill hold all the companyIDs like thisuser_array = input_json.map((elem)=>elem.userID) // which will hold all the company ID //For the above input_json, the value of `user_array` would be // `user_array: [2,1]` company_array = input_json.map((elem)=>elem.userID) //For the above input_json, the value of `company_array` would be // `company_array`:[4,4]After I got both the values of userIDs & companyID separately, I am firing a query on MongoDB using the $in to match id from array
db.collection.find({ userID:{ $in:user_array }, accounts.companyID:{ $in:company_array } })I am not getting the desired results, I want the userID and companyID to be used as an unique key for getting the data.
But what is happening is it is matching the userID in user_array and companyID in company_array.However, I want both userID and companyID to be checked for getting the data.
Any help will be much apreciated :)
{ $or: arr }here. That would be the AND of each "set" of conditions, where as$inapplies all different sets of combinations over two values. Show documents you would expect to match if you are still unsure"listOfFields"then how are you supposed to know which one searches the root and which searches the array? In all honesty, the real problem here is you have real data you are not showing us. Trying to express it in an abstract way is simply not coming across in a clear manner. Better to show something more representative of your actual data and a real request, otherwise it's likely any response you get will also be very wrong.