MongoDb Node.js Querying Nested Objects not working? - javascript

I need to query for 2 dynamic properties using the node.js driver for mongodb.
Here's the data structure:
{
"_id":"123456",
"dateAdded":"2017-09-20T08:36:40.325Z",
"followers":{
"name1":{
"followedOn":"2017-09-20T08:36:40.325Z",
"unfollowedOn":null
},
"name2":{
"followedOn":"2017-09-20T08:36:40.325Z",
"unfollowedOn":null
}
}
}
Here's my code:
//Working but not dynamic
collections.find({ '_id': '123456', 'followers.name1': { $exists: false } })
//My failed attempt at making it dynamic
const id = "123456"
const username = "name1"
let query = {}
query['followers.'+username] = { $exists: true }
collections.find( { "_id": id, query }
Note that this is not a duplicate of "how to make a dynamic key in an object literal". The .find() method of node.js mongodb driver does not accept object literals. I can't find documentation of what it accepts exactly.

Your _id property needs to be within query object, not separate.
Here's how to do it:
let query = { _id: id };
query['followers.'+username] = { $exists: true }
collections.find(query);

Related

How to find all users that have emails in an array in prisma using Typescript?

Hi sorry for the confusing question. Basically, I want to find all the users that have emails that are included in the array.
For example I have an array:
const arr = ['test#gmail.com', 'test2#gmail.com']
and I have my model
model User {
id Int
username String
name String
email String
}
I want to do something like
const data = await prisma.user.findMany({
where : {
email: in arr
},
select : {
id: true
}
});
I have been trying to find a workaround for this for the longest time.
This is the syntax for the in operator in Prisma.
const arr = ['test#gmail.com', 'test2#gmail.com']
const data = await prisma.user.findMany({
where : {
email: {in: arr}
},
select : {
id: true
}
});
You can read more in the Prima Client Reference in the docs.

How to populate an array inside a map function in js and send it to the server?

This is my ObjectIds array -
obj_ids = [
"5ee71cc94be8d0180c1b63db",
"5ee71c884be8d0180c1b63d9",
"5ee71c494be8d0180c1b63d6",
"5ee71bfd4be8d0180c1b63d4"
]
I am using these objectids to serach whether they exist in the db or not and based on that I want to send the response to server.
This is the code I am trying but I dont know how to populate the array and send it to the server.
var msg = [];
obj_ids.map((ele) => {
Lead.find({ _id: ele._id }, async function (error, docs) {
if (docs.length) {
msg.push(
`Lead already exist for Lead id - ${ele._id} assgined to ${docs[0].salesPerson}`
);
} else {
msg.push(`Lead doesn't exist for Lead id: ${ele._id}`);
const newDuty = new AssignedDuty({
duty: ele._id,
salesPerson: req.body.salesPerson,
});
await newDuty.save();
}
});
});
res.json(msg);
By doing this approach I am getting an empty array. I cannot put res.json(msg) inside the loop. If it is possible by using async-await, please guide me through.
You don't need to make multiple queries to find whether given object ids exist in the database.
Using $in operator, you can make one query that will return all the documents where the _id is equal to one of the object id in the list.
const docs = await Lead.find({
_id: {
$in: [
"5ee71cc94be8d0180c1b63db",
"5ee71c884be8d0180c1b63d9",
"5ee71c494be8d0180c1b63d6",
"5ee71bfd4be8d0180c1b63d4"
]
}
});
After this query, you can check which object id is present in the docs array and which is absent.
For details on $in operator, see $in comparison operator
Your code can be simplified as shown below:
const obj_ids = [
"5ee71cc94be8d0180c1b63db",
"5ee71c884be8d0180c1b63d9",
"5ee71c494be8d0180c1b63d6",
"5ee71bfd4be8d0180c1b63d4"
];
const docs = await Lead.find({
_id: { $in: obj_ids }
});
const msg = [];
obj_ids.forEach(async (id) => {
const doc = docs.find(d => d._id == id);
if (doc) {
msg.push(
`Lead already exist for Lead id - ${doc._id} assgined to ${doc.salesPerson}`
);
}
else {
msg.push(`Lead doesn't exist for Lead id: ${id}`);
const newDuty = new AssignedDuty({
duty: id,
salesPerson: req.body.salesPerson
});
await newDuty.save();
}
});
res.json(msg);

update nested Object data without changing Object Id

I am currently using array filters to update the nested object.
My structure is -
Category Collection -
{
name:Disease,
_id:ObjectId,
subCategory:[{
name:Hair Problems,
_id:ObjectId,
subSubCategory:[{
name: Hair Fall,
_id:ObjectId
},{
name: Dandruff,
_id:ObjectId
}]
}]
}
I want to update the subsubcategory with id 1.1.1 which I am doing by using array filters.
let query = { 'subCategories.subSubCategories._id': subSubId };
let update = { $set: { 'subCategories.$.subSubCategories.$[j]': data } };
let option = { arrayFilters: [{ 'j._id': subSubId }], new: true };
await Categories.findOneAndUpdate(query, update, option
This code is working fine but array filters change the object id of subsubCategory. Is there any other alternative to do so without changing the ObjectId.
Thanks in advance
You can loop over the keys which you are getting as payload and put inside the $set operator.
const data = {
firstKey: "key",
secondKey: "key2",
thirdKey: "key3"
}
const object = {}
for (var key in data) {
object[`subCategories.$.subSubCategories.$[j].${key}`] = data[key]
}
let query = { 'subCategories.subSubCategories._id': subSubId };
let update = { '$set': object };
let option = { 'arrayFilters': [{ 'j._id': subSubId }], 'new': true };
await Categories.findOneAndUpdate(query, update, option)
Problem is in $set line there you have not mentioned specific fields to be update instead subCategory.$.subSubCategory.$[j] will replace complete object element that matches the _id filter. Hence your _id field is also getting updated. You have to explicitly mention the field name after array element identifier. See example below:
Suppose you want to update name field in subSubCategories from Dandruff to new Dandruff. Then do this way:
let update = { $set: { 'subCategories.$.subSubCategories.$[j].name': "new Dandruff" } };
This will only update name field in subSubCategories array

.find() nested object using dynamic property names

I need to find nested objects in MongoDb using the Node.js Driver.
I'm having trouble accessing nested properties when the property name is dynamic. Here's my code:
//This gives expected results but "name1" isn't dynamic
collection.find({ 'followers.name1': { $exists: false } })
//Here's what I tried that does not give expected results
const username = "name1"
let query = { followers: {} }
query.followers[username] = { $exists: false }
collection.find(query)
Here's an example of the database structure:
{
"_id":"xxxxxxxxxxx",
"dateAdded":"2017-09-20T08:36:40.325Z",
"followers":{
"name1":{
"followedOn":"2017-09-20T08:36:40.325Z",
"unfollowedOn":null
},
"name2":{
"followedOn":"2017-09-20T08:36:40.325Z",
"unfollowedOn":null
}
}
}
Edit: my question is not a duplicate of the one marked as a duplicate. MongoDb find() argument is not an object literal. That's the whole point of my question, using like it like an object literal doesn't work.
I found the solution myself in the end. The key needs to be a string so you need to do:
const username = 'name1'
let query = {}
query['followers.'+username] = { $exists: false }
collection.find(query)

MongoDB and nodejs, find throught list of ids

I have two collections:
users:
{
_id: ObjectId('123...'),
docs: [
ObjectId('512d5793abb900bf3e000002'),
ObjectId('512d5793abb900bf3e000001')
]
}
docs:
{
_id: ObjectId('512d5793abb900bf3e000002'),
name: 'qwe',
...
}
{
_id: ObjectId('512d5793abb900bf3e000001'),
name: 'qwe2',
...
}
I want to get docs from ids. I try this solution, but I get this message:
{ db: { domain: null,
_events: {},
_maxListeners: 10,
databaseName: 'test', ...
Your message looks like a mongodb cursor returned from find by native mongodb driver.
To get actual data you should use toArray function of the cursor:
var ObjectID = require('mongodb').ObjectID;
// you shall wrap each id in ObjectID
var idsProjects = [
ObjectID('512d5793abb900bf3e000002'),
ObjectID('512d5793abb900bf3e000001')
];
collectionProjects.find({
_id: { $in: idsProjects }
},{
_id: -1, // use -1 to skip a field
name: 1
}).toArray(function (err, docs) {
// docs array here contains all queried docs
if (err) throw err;
console.log(docs);
});
But I recommend you to switch from native mongodb driver to some wrapper around it like monk.
If you care about the order of the list, the answer of Mr.Leonid may not work as expected to do.
That's because find gets the docs that have _id equals to any _ids $in the list so the output docs will be ordered by the main order of the collection itself not the order of the input list.
To solve that you can just use the normal findOne with a for loop to the list.
The code will look like:
var ObjectID = require('mongodb').ObjectID;
var idsProjects = [
'512d5793abb900bf3e000002',
'512d5793abb900bf3e000001'
];
let usersList = new Array();
for (let index = 0; index < idsProjects.length; index++) {
const myID = idsProjects[index];
const query = { _id: ObjectID(myID) };
const options = {
projection: {name: 1 };
var user= await collectionProjects.findOne(query,options);
usersList.push(user);
}
// that's it,
// here we have a list of users 'usersList'
//with same order of the input ids' list.
console.log(usersList);

Categories