Im trying to get iterate the results array variable.
I am able to get the array values inside the function, but when i try to log it outside it shows null.
Wen i googled few posts, i see that forEach was not recommended, suggested was for loop, i tried even that and i get null for result.
What is the issue in cursor.forEach()...
router.get('/getlist', function(req, res, handleError) {
client.connect('mongodb://localhost', function(err, client) {
if (err) throw err;
var db = client.db('angular-demo');
var collection = db.collection("api_details");
var query = {};
var cursor = collection.find(query);
var results = new Array();
var results = cursor.forEach(
function(result) {
return result;
console.log("insert")
console.log(results);
}
);
console.log("append")
console.log(results); //results here shows null
});
});
Log result:
append
[]
insert
[ { _id: 5a6867c8e54f6120709eabc5,
app_id: 'CaseRegistration',
description: 'API to register cases in the system',
cost_per_usage: '0.5',
__v: 0 } ]
insert
[ { _id: 5a6867c8e54f6120709eabc5,
app_id: 'CaseRegistration',
description: 'API to register cases in the system',
cost_per_usage: '0.5',
__v: 0 },
{ _id: 5a6867fde54f6120709eabc6,
app_id: 'CheckCreation',
description: 'CREs create the case with minimal data and assigns it to case initiation team to create checks',
cost_per_usage: '1',
__v: 0 } ]
If you want to grab results from find you can use toArray
cursor.toArray(function (error, documents) {
console.log(documents)
})
// or
cursor.toArray().then(function (documents) {
console.log(documents)
})
// or in async function
const documnets = await cursor.toArray()
or if you need to transform them somehow use map
It's not relevant if you use forEach or for loop in this case. The results variable isn't being populated because you're trying to populate it with the return result from the forEach which doesn't return anything useful.
What you want to do, is just iterate over the results, and fill the results array while iterating.
Something more like this :
var results = [];
cursor.forEach(
function(result) {
results.push(result);
}
);
console.log(results); // This should be populated now
Related
I am getting data like this:
This is the code :
User.find({ Username: user }, function(err, found_user) {
console.log('user data'+ found_user );
if(found_user.length > 0){
console.log('inside found user');
var recordings = found_user.recordings;
console.log(recordings)
for (var singleRecords in recordings){
console.log("Single record :"+singleRecords);
if(!singleRecords.isPlayed){
console.log(singleRecords.playingUrl);
twiml.play(singleRecords.playingUrl);
found_user.recordings[singleRecords].isPlayed = true;
found_user.save(function (err) {
if(err)
throw err
});
}
}
}
And this is the value of found User :
user data { Username: 'B',
__v: 2,
_id: 58ac15e4b4e1232f6f118ba3,
recordings:
[ { isPlayed: false,
playingUrl: 'http://localhost:8000/public/toplay/playing_file_1487672817599.mp3' },
{ isPlayed: false,
playingUrl: 'http://localhost:8000/public/toplay/playing_file_1487672827411.mp3' } ]
}
inside found user
in variable found_user. But it is not giving me any data inside it. Like found_user.Username gives undefined value.
I want to store that recordings array inside a variable. Any idea how to do it ?
find() returns an array of docs that match the criteria in the callback hence the line
var recordings = found_user.recordings;
will not work as it's expecting a Document not an array.
You could use findOne() method which returns a document as:
User.findOne({ Username: user }.exec(function(err, found_user) {
console.log('user data'+ found_user );
if (found_user) {
console.log('inside found user');
var recordings = found_user.recordings;
console.log(recordings);
}
});
I have a collection of documents with many different characteristics. I want to get from Mongoose an array where each item in the array is a unique value for an attribute, in this case color. In other words, I do not want an array of each item, just of the color values. So in this case, if I have 100 different products, but only 8 colors between all 100, then I would like the to have an array of the 8 colors. Here is the way I have been using but I am wondering if there is a more concise way of doing it?
var allproducts = Products.find(function (err, products) {
// Get all products
var collection = [];
// Make an array to contain a product with each of the 8 colors
var uniqueArray = [];
// Make an array to check the current color against (if already used)
for (var i = products.length - 1; i >= 0; i--) {
if (uniqueArray.indexOf(products[i].color) === -1) {
// Check to see if the color is in the unique color array
uniqueArray.push(products[i].color);
// Add it if not
collection.push(products[i]);
// Add the product to the collection array if its color is not in the unique array
}
}
});
Attempting to use the Mongoose aggregate method:
router.get('/', function (req, res) {
Products.aggregate([{
$group: {
_id: '$color'
}
}], {}, function (err, collection) {
console.log(err, collection);
if (err) {
throw err
}
res.end();
// res.json(collection);
});
});
This can be easily solved using lodash
npm install lodash --save
var _ = require('lodash');
var allproducts = Products.find(function (err, products) {
...
var uniqueArray = _.pluck(products, 'color');
uniqueArray = _.uniq(uniqueArray);
....
});
I. Directly in mongodb, you have to this request :
db.colors.aggregate( { $group: {_id:"$color"} } )
Result :
{ "_id" : "yellow" }
{ "_id" : "blue" }
{ "_id" : "red" }
MongoDB Tutorial for aggregation
II. In mongoose you can do :
Products.aggregate(
{$group: {
_id: '$color' // grouping key - group by field district
}
}).exec( function( err, products ) {
if ( err ) {
return res.status( 400 ).send({
message: errorHandler.getErrorMessage( err )
});
} else {
res.json( products );
}
});
Mongoose : Model aggregate
I am using findOneAndUpdate in a forEach loop to create/update multiple entries.
I would like it to return an array of all the object id's it has created or updated.
During the loop, I can see it adding data to the array, but one it leaves the loop, the array is empty.
Should the array not be populated?
here is my code.
var softwareArray = ["Software1","Software2","Software3"],
updatedArray = [];
softwareArray.forEach(function(software){
Software.findOneAndUpdate(
{
Name: software
},
{
Name: software
},
{upsert:true},
function(err, rows){
updatedArray.push(rows._id);
console.log(updatedArray); //This has data in it....
}
);
});
console.log(updatedArray); //This has no data in it...
Edit: Updated with my working changes for Thiago
var softwareArray = ["Software1","Software2","Software3"],
updatedArray = [];
loopSoftware(softwareArray, function(updatedArray){
console.log(updatedArray);
//carry on....
}
function loopSoftware(input, cb){
var returnData = [];
var runLoop = function(software, done) {
Software.findOneAndUpdate(
{Name: software},
{Name: software},
{upsert:true},function(err, rows){
returnData.push(rows._id);
done()
}
);
};
var doneLoop = function(err) {
cb(returnData);
};
async.forEachSeries(input, runLoop, doneLoop);
}
I decorated your code to make you see when what is happening:
var softwareArray = ["Software1","Software2","Software3"],
updatedArray = [];
// TIMESTAMP: 0
softwareArray.forEach(function(software){
// TIMESTAMP: 1, 2, 3
Software.findOneAndUpdate(
{
Name: software
},
{
Name: software
},
{upsert:true},
function(err, rows){
// TIMESTAMP: 5, 6, 7
updatedArray.push(rows._id);
console.log(updatedArray); // This has data in it....
// want to use the result?
if (updatedArray.length == softwareArray.length) {
console.log(updatedArray);
}
}
);
});
// TIMESTAMP: 4
console.log(updatedArray);
Of course this will happen - just like any other networking on Node, it's asynchronous!
This means that the callback you specified for your findOneAndUpdate operation have not run yet when it reaches the console.log(updatedArray); code.
Take a look at Q for working around this common problem.
I really really need help in this. I am using node.js with mongodb and mongoose. So far I've managed to create a schema and save those into my database.
var Bericht = new Schema({
name : String
, mail : String
, betreff : String
, inhalt : String
, datum : Date
});
var Bericht = mongoose.model('Bericht', Bericht);
I habe a html formular where I can transmit with misc. fields data, by querystring I converting those into readable strings
var bericht_data = {
name: tempo.Name
, mail: tempo.Mail
, betreff: tempo.Betreff
, inhalt: tempo.Inhalt
};
var testoro = new Bericht(bericht_data);
testoro.save(function (err) {
if (!err) console.log('Success!');
});
so tempo.Name for example is a string and it also successful in saving it.
So far I can save all data from this formular into my mongodb.
Now the very problem: I want the data back as string to handle for dynamic html.
To get the info into my console, I use
Bericht.find(
{},
{ '_id': 0},
function(err, docs) {
if (!err){
console.log(docs);
// process.exit();
}
else { throw err;}
}
);
The console gives me all data which was ever saved in my schema Bericht excluding the long _id stuff. Sample output:
[ { name: 'Hans', mail: 'hans#wurst.de', betreff: 'I lost my wurst', inhalt: 'look at me, I am amazing' } ]
That's just one, normally there would be a huge amount of data.
The idea is right now to extract only the name into a string like "Hans". I want to get this name into a var, but hell it seems impossible!
I've tried
Bericht.find(
{},
{ '_id': 0},
function(err, docs) {
if (!err){
console.log(docs.name);
// process.exit();
}
else { throw err;}
}
);
But the I get only "undefined" delivered. I appreciate your help!
Take a look at Mongoose QueryStreams. I haven't used it myself, but I've modified their example code to fit your Model, to give you an idea of how it might work in practice:
var stream = Bericht.find().stream()
, names = []
, i;
function closeHandler() {
console.log(JSON.stringify(names));
};
stream.on('data', function (doc) {
if (doc.name) {
names.push(doc.name);
}
})
stream.on('error', function (err) {
// handle err
})
stream.on('close', closeHandler)
Mongoose find return an array of documents so you should try the following:
Bericht.find(
{},
{ '_id': 0},
function(err, docs) {
if (!err){
for(var i=0; i<docs.length; i++)
{
console.log(docs[i].name);
}
// process.exit();
}
else { throw err;}
}
);
I have an array of _ids and I want to get all docs accordingly, what's the best way to do it ?
Something like ...
// doesn't work ... of course ...
model.find({
'_id' : [
'4ed3ede8844f0f351100000c',
'4ed3f117a844e0471100000d',
'4ed3f18132f50c491100000e'
]
}, function(err, docs){
console.log(docs);
});
The array might contain hundreds of _ids.
The find function in mongoose is a full query to mongoDB. This means you can use the handy mongoDB $in clause, which works just like the SQL version of the same.
model.find({
'_id': { $in: [
mongoose.Types.ObjectId('4ed3ede8844f0f351100000c'),
mongoose.Types.ObjectId('4ed3f117a844e0471100000d'),
mongoose.Types.ObjectId('4ed3f18132f50c491100000e')
]}
}, function(err, docs){
console.log(docs);
});
This method will work well even for arrays containing tens of thousands of ids. (See Efficiently determine the owner of a record)
I would recommend that anybody working with mongoDB read through the Advanced Queries section of the excellent Official mongoDB Docs
Ids is the array of object ids:
const ids = [
'4ed3ede8844f0f351100000c',
'4ed3f117a844e0471100000d',
'4ed3f18132f50c491100000e',
];
Using Mongoose with callback:
Model.find().where('_id').in(ids).exec((err, records) => {});
Using Mongoose with async function:
const records = await Model.find().where('_id').in(ids).exec();
Or more concise:
const records = await Model.find({ '_id': { $in: ids } });
Don't forget to change Model with your actual model.
Combining Daniel's and snnsnn's answers:
let ids = ['id1', 'id2', 'id3'];
let data = await MyModel.find({
'_id': {
$in: ids
}
});
Simple and clean code. It works and tested against:
"mongodb": "^3.6.0",
"mongoose": "^5.10.0",
Use this format of querying
let arr = _categories.map(ele => new mongoose.Types.ObjectId(ele.id));
Item.find({ vendorId: mongoose.Types.ObjectId(_vendorId) , status:'Active'})
.where('category')
.in(arr)
.exec();
This code works for me just fine as of mongoDB v4.2 and mongoose 5.9.9:
const Ids = ['id1','id2','id3']
const results = await Model.find({ _id: Ids})
and the Ids can be of type ObjectId or String
Both node.js and MongoChef force me to convert to ObjectId. This is what I use to grab a list of users from the DB and fetch a few properties. Mind the type conversion on line 8.
// this will complement the list with userName and userPhotoUrl
// based on userId field in each item
augmentUserInfo = function(list, callback) {
var userIds = [];
var users = []; // shortcut to find them faster afterwards
for (l in list) { // first build the search array
var o = list[l];
if (o.userId) {
userIds.push(new mongoose.Types.ObjectId(o.userId)); // for Mongo query
users[o.userId] = o; // to find the user quickly afterwards
}
}
db.collection("users").find({
_id: {
$in: userIds
}
}).each(function(err, user) {
if (err) {
callback(err, list);
} else {
if (user && user._id) {
users[user._id].userName = user.fName;
users[user._id].userPhotoUrl = user.userPhotoUrl;
} else { // end of list
callback(null, list);
}
}
});
}
if you are using the async-await syntax you can use
const allPerformanceIds = ["id1", "id2", "id3"];
const findPerformances = await Performance.find({
_id: {
$in: allPerformanceIds
}
});
I tried like below and it works for me.
var array_ids = [1, 2, 6, 9]; // your array of ids
model.find({
'_id': {
$in: array_ids
}
}).toArray(function(err, data) {
if (err) {
logger.winston.error(err);
} else {
console.log("data", data);
}
});
I am using this query to find the files in mongo GridFs. I wanted to get the by its Ids.
For me this solution is working: Ids type of ObjectId.
gfs.files
.find({ _id: mongoose.Types.ObjectId('618d1c8176b8df2f99f23ccb') })
.toArray((err, files) => {
if (!files || files.length === 0) {
return res.json('no file exist');
}
return res.json(files);
next();
});
This is not working: Id type of string
gfs.files
.find({ _id: '618d1c8176b8df2f99f23ccb' })
.toArray((err, files) => {
if (!files || files.length === 0) {
return res.json('no file exist');
}
return res.json(files);
next();
});