Mongoose find not match ids from array which passed - node.js

I'm stuck in mongoose query. I've an array of ids as input, I want to search that array of ids in a database for matching elements. It returns perfect result after using $in in find query.
Now, I want that Ids from an array which is not found in the database. what is the best way to do it?
Please try to comment it.
Template.find({
_ids : [
"as6d87as67da7s8d87a87", // available in database
"as6dasd8sa9d8a9a9s8d7", // not-available in database
"6756asd5as6dsadghasd3", // available in database
]
}, function(err, result){
// Need result as "as6dasd8sa9d8a9a9s8d7", which is not matched. or in object.
// Can we do with `aggregate` query?
});

I think this makes what you want
var _ids = [
"as6d87as67da7s8d87a87",
"as6dasd8sa9d8a9a9s8d7",
"6756asd5as6dsadghasd3"
];
Template.find({
_ids : _ids
}, function(err, result){
var filteredResult = _ids.filter(currentId => !result.some(item => item._id.toString() == currentId));
});

Answer by David will work. The idea here is to compare _ids array with the _ids in the result and return the missing ones. Adding more code just for understanding purposes:
Push _id of result into a new array
Compare _ids and the new array to return missing elements
var resIDs = new Array();
for (var i = 0; i < result.length; i++) {
resIDs.push(result[i]._id.toString());
}
var resultFiltered =
_ids.filter(function (v) {
return !resIDs.includes(v.toString());
})

Related

Using NodeJS with monobd ($all function)

I am implementing a search by keywords with mongodb and node JS, but I see that there is the operator $all in Mongo that selects the documents where the field holds an array and contains all elements.
Here is my source code with node JS
exports.find = function(req, res) {
var b=req.params.search;
var query = {};
var cadsrch = b.split(' ');
var l = cadsrch.length;
var i = 0;
for (i = 0; i < l; i++) {
if(cadsrch[i]!=''){
query[i]=new RegExp('^'+cadsrch[i], 'i');
}
}
db.collection('publicacion', function(err, collection) {
collection.find({tags: {'$all':query}},{title:true,content:true}).limit(5).toArray(function(err, items) {
res.jsonp(items);
});
});
};
The above source does not work, but this query works in Mongo->
db.publication.find({tags:{$all:['chevrolet','car']}})
and the strange thing is with '$in' instead '$all' works, but it would be useful to work with '$all' to implement exact searches
The query below is working because you're not using Regexp in the $all array:
db.publication.find({tags:{$all:['chevrolet','car']}})
I believe adding RegExp into $all is forcing the query to find an array element that contains all of your expressions together.
For example, execute below:
db.publicacion.find({"tags": {$all: [/chevrolet/i, /car/i]}})
This will not return any result because it will search for a tag that contains both chevrolet and car keywords into a single string like: ["chevrolet-cars", "chevrolet-car", "carchevrolet", "chevroletcar"].
If you want to use multiple RegExp to find the documents that have similar tags, use Mongoose Aggregate functions: https://mongoosejs.com/docs/api/aggregate.html

How to search data in mongodb with dynamic fields using mongoose?

I've a node.js api in which user sends the required fields as an array to be fetched from the mongodb database. I need to find the data of that fields using Find query. I've written forEach statement to loop through that array and got the array elements. But when I try to get the results by inserting the array elements in the query, it doesn't giving the required results. Could any one please help me in resolving the issue by seeing the code below?
templateLevelGraphData: async function(tid,payload){
let err, templateData, respData = [], test, currentValue;
[err,templateData] = await to(Template.findById(tid));
var templateId = templateData.templateId;
payload.variables.forEach(async data=>{
console.log(data); //data has the array elements like variables=["humidity"]
[err, currentValue] = await to(mongoose.connection.db.collection(templateId).find({},{data:1}).sort({"entryDayTime":-1}).limit(1).toArray());
console.log(currentValue);
});
return "success";
}
The expected output is,
[ { humidity: 36 } ]
But I'm getting only _id like,
[ { _id: 5dce3a2df89ab63ee4d95495 } ]
I think data is not applying in the query. But I'm printing the data in the console where it's giving the correct results by displaying the array elements like, humidity. What I need to do to make it work?
When you are passing {data: 1} you are passing an array where is expecting name of column.
You have to create an object where the keys are going to be the elements of the array and set them to 1.
const projection = data.reduce((a,b) => (a[b]=1, a), {});
[...] .find({}, projection) [...]
Actually I got the solution.
for(let i=0;i<payload.variables.length;i++){
var test = '{"'+ payload.variables[i] +'":1,"_id":0}';
var query = JSON.parse(test);
[err, currentValue] = await to(mongoose.connection.db.collection(templateId).find({"deviceId":deviceId},query).sort({"entryDayTime":-1}).limit(1).toArray());
console.log(currentValue); //It's giving the solution
}

CoreMongooseArray to Normal Array

I'm shortlisting the 2 elements from one schema and want to update in another schema. for that i used slice method to shortlist first 2 elements from an array. but am Getting
CoreMongooseArray ['element1','element2']
instead of ["element1", "element2"]
How do i remove "CoreMongooseArray" ?
connection.connectedusers.find({}, async (err, docs) => {
if(err) throw err;
var users = docs[0].connectArray;
if (docs[0] != null && users.length >= 2) {
var shortListed = users.slice(0, 2);
try {
await connection.chatschema.updateMany({}, { $push: { usersConnected: [shortListed] } }, { upsert: true });
} catch (err) {
res.status(201).json(err);
}
}
You need to add lean() to your query.
From the docs:
Documents returned from queries with the lean option enabled are plain javascript objects, not Mongoose Documents. They have no save method, getters/setters, virtuals, or other Mongoose features.
If you already got the mongoose array and like to convert to simple js array
const jsArray = mongooseArray.toObject();
https://mongoosejs.com/docs/api/array.html#mongoosearray_MongooseArray-toObject
For some reason .toObject() didn't work for me. lean() option works, but it's not suitable when you already have an object with mongoose array in it. So in case if you already have mongoose array and you want just to convert it to plain js array you can use following code:
function mongooseArrayToArray(mongooseArray) {
const array = [];
for (let i = 0; i < mongooseArray.length; i += 1) {
array.push(mongooseArray[0]);
}
return array;
};
usage:
const array = mongooseArrayToArray(mongooseArray);
If you just want to convert the CoreMongooseArray to a normal array without changing anything else:
const jsArray = [...mongooseArray];

Aggregate returns empty array

Using mongoose's Model.aggregate() returns an empty array.
I've essentially copied the format as seen here.
var match = {};
var project = {};
project["_id"] = 0;
project["products.totalprice"] = 1;
project["line"] = "$products.closedate";
ThisCollection.aggregate([
{$match: match},
{$project: project},
{$group: {
_id: "$line"
}}
], function(err, docs){
console.log(docs); //!! Returning []
});
My schema is essentially a name and _id field with a nested array of products with totalprice, closedate and some other fields.
There are most certainly a plethora of entries (some 130,000 records). Can anyone spot an issue with this?
I have created this dummy data to represent a skeleton of your schema:
db.data.save({name:"a",products:{totalprice:1,closedate:1}})
db.data.save({name:"b",products:{totalprice:2,closedate:2}})
This query does return two records and is identical to yours when inserting the JSON string for the JavaScript variable project
ThisCollection.aggregate([{$match:{}},{$project:{"_id":0,"products.totalprice":1,line:"$products.closedate"}},{$group:{_id:"$line"}}])

Array empty after a push operation inside Mongoose findbyId operation

I am trying to insert objects to a array after a Mongoose FindById operation.It is showing empty after the for loop execution.Please help.I have attached a snippet below.Thanks
var list=[]
for( var i=0;i<groupdata.length;i++){
Group.findById(groupdata[i],function(err,group){
if(err){
return callback(err,null)
}
for(var j=0;j<group.members.length;j++){
list.push(group.members)
}
});
}
console.log(list)---->showing empty array
It's hard to test without more info but you may just need to change it to:
var list=[]
for( var i=0; i < groupdata.length; i++){
Group.findById(groupdata[i],function(err,group){
if(err){
return callback(err,null)
}
for(var j=0;j<group.members.length;j++){
list.push(group.members[j])
}
});
}
console.log(list)
The only difference is in the list.push(group.members) line. I added [j]. It's also very possible that you could simplify your code using $in and the underscore.js library:
Group.find({_id: {$in: groupdata}}).
exec(function(err, groups) {
//check out underscore.js's pluck for a quick way to do what you're after
var list = _.flatten(_.pluck(groups, 'members'));
});

Resources