Mongoose, MongoDb, Node. Mongoose object not registering find()
Tag : node.js , By : Vijayant Singh
Date : March 29 2020, 07:55 AM
may help you . When i'm getting to my routes and requesting to getUSers from my mongoDB it says the User.find() is not defined. Sorry in advance if i use incorrect terminology i'm jumping in face first. , Change the line where you require User to var User = require('./node_modules/user-app/user').User;
module.exports = {User:User};
|
Fetch entries from mongodb using mongoose
Date : March 29 2020, 07:55 AM
it helps some times According to Mongo docs, you should pass either a string or javascript function to $where, and you pass javascript expression, which gets evaluated in place. Also, your query can be simplified to PostModel.Post.find({
activeFlag: true,
tags: {
$exists: true,
$not: /^(inc:|ecx:)/
}
}, function(err, stack) {
if (!err) {
logger.debug('Posts found successfully.');
} else {
logger.error('Problem in finding posts, error :' + err);
}
});
|
Date : March 29 2020, 07:55 AM
I wish this helpful for you Try to do it through mongoose populate and aggregate. Sample codes as below. var Post = mongoose.model('Post', PostSchema);
var Comment = mongoose.model('Comment', CommentSchema);
Comment.aggregate([
{$group: {_id: '$_post', comments: {$push: '$body'}}}
// ...
], function(err, result) {
if (err)
// error handling
Post.populate(result, {path: "_id"}, function(err, ret) {
if(err)
console.log(err);
else
console.log(ret);
});
});
|
Cannot fetch from or insert data to mongodb using mongoose
Date : March 29 2020, 07:55 AM
it fixes the issue I figured out the issue, it wasn't an issue with the credentials. It was an issue with the mongoose version. The mongoose version I used didn't support the authentication. I had to remove the package and reinstall the latest version. with node install mongoose@latest
|
Duplicate Objects after creating object within object in Mongoose (MongoDB, Node.JS, express)
Date : March 29 2020, 07:55 AM
wish helps you Because Array.forEach is blocking for asynchronous methods, you are getting duplicates. For an asynchronous-friendly version of Array.forEach, you can the async module or Promises. The latter can be followed with this example (untested): let regionsPromise = Region.insertMany(dummyregions);
let productsPromise = Product.insertMany(dummyproducts);
Promise.all([regionsPromise, productsPromise])
.then(([regions, products]) => {
console.log(regions); // check regions array
console.log(products); // check products array
let ids = regions.map(r => r._id);
console.log(ids); // check ids
Region.update(
{ '_id': { '$in': ids } },
{ '$push': { 'products': { '$each': products } } },
{ 'multi': true }
);
})
.catch((err) => {
/* Error handling */
console.error(`Error ${err.message}`);
});
(async () => {
try {
const regions = await Promise.all(dummyregions.map(async (reg) => {
await Region.create(reg);
}));
const products = await Promise.all(dummyproducts.map(async (prod) => {
await Product.create(prod);
}));
for (let region of regions) {
await region.findByIdAndUpdate(region._id, {
'$push': {
'products': { '$each': products }
}
});
}
/*
if (regions && regions.length) {
await Promise.all(
regions.map(async (r) => {
await r.findByIdAndUpdate(r._id, {
'$push': {
'products': { '$each': products }
}
});
})
);
}
*/
} catch (err) {
console.error(`Error ${err.message}`);
}
})();
|