mongoose-populate

NodeJs api with mongoose any request go to another collection efficient

主宰稳场 提交于 2021-01-29 15:15:20
问题 I'm write nodeJs api for app with users and I want for each user to use another mongo collection. I recognize each user with the URL address in the params fields. everything is work fine. But when I go to collection dynamically it's very slow. any one idea how to do this faster? thanks in advance ;) app.js this code do req in 2.5 seconds POST /login 200 2487.531 ms - 206 app.use("/:businessUrl", (req, res, next) => { console.log(req.params.businessUrl); mongoose .connect(process.env.MONGO_URI

Is it possible to have condition in mongoose populate

坚强是说给别人听的谎言 提交于 2021-01-29 03:29:36
问题 I have a sub document called services in a collection it contains serviceID and type . serviceID is referred to two collections internalservice and externalservice varies based on the type field. internalServices:[{ serviceID:ObjectId('0000000000000'), serviceDesc:'sweeping' }, { serviceID:ObjectId('0000000000000'), serviceDesc:'floor cleaning' }] externalServices:[{ serviceID:ObjectId('0000000000000'), serviceDesc:'painting' }, { serviceID:ObjectId('0000000000000'), serviceDesc:'white

How to populate one to many relationship in mongoose with parent refrencing?

会有一股神秘感。 提交于 2021-01-28 21:31:06
问题 I have three collections: parent, child1, child2. parent collection: [{_id:1}, {_id:2}, {_id:3}] child1 collection: [{_id:1, child1:'a', parent:1}, {_id:2, child1:'b', parent:1}, {_id:3, child1:'c', parent:2}] child2 collection: [{_id:1, child1:'d', parent:2}, {_id:2, child1:'e', parent:3}] collections child1 and child2 referenced to parent collection. now, I want a query in mongoose to get a result like this: [ { _id:1, child1:[{_id:1, child1:'a'}, {_id:2, child1:'b'}], child2:[], }, { _id:2

Unrecognized pipeline stage name: '$populate'

故事扮演 提交于 2020-06-29 03:35:06
问题 I was using the following code to receive my data set and it worked fine without any error. const postslist = await postSchemaModel.find() .limit(limit) .skip(startIndex) .sort({ createdAt: -1 }) .populate(user_id) .populate("user_id", "img user_name _id"); But , In my case I want to get the result set with $geonear and I used the following code. There it gives me this error * Unrecognized pipeline stage name: '$populate'* But the populate function was working fine with the above code. Below

Unrecognized pipeline stage name: '$populate'

痴心易碎 提交于 2020-06-29 03:35:01
问题 I was using the following code to receive my data set and it worked fine without any error. const postslist = await postSchemaModel.find() .limit(limit) .skip(startIndex) .sort({ createdAt: -1 }) .populate(user_id) .populate("user_id", "img user_name _id"); But , In my case I want to get the result set with $geonear and I used the following code. There it gives me this error * Unrecognized pipeline stage name: '$populate'* But the populate function was working fine with the above code. Below

MongoDB $lookup vs Mongoose populate

≯℡__Kan透↙ 提交于 2020-06-28 05:19:27
问题 I have seen this and other similar titled questions, none answer my question. I was going through the mongoose documentation where I read MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections. How does populate() in mongoose work that makes it more powerful than MongoDB's $lookup ? Isn't mongoose a tool that helps nodejs users work with mongodb. If so how

mongoose: populate in mongoose which doesn't have any ObjectId

元气小坏坏 提交于 2020-05-16 03:54:27
问题 I have 2 schemas as shown const gameSchema = new mongoose.Schema({ title: String, rating: { type: Number, min: 0, max: 100 }, genres: { type: Array, ref: 'Genres' } }) const GenreSchema = new mongoose.Schema({ id: { type: Number }, name: String, description: String }) mongoose.model('Games', gameSchema) mongoose.model('Genres', GenreSchema) Now one endpoint /api/games returns an array of results from games where genres property contains array of id eg. "genres": [4, 7, 19] How do I populate

How to lookup a field in an array of subdocuments in mongoose?

与世无争的帅哥 提交于 2020-04-18 05:47:25
问题 I have an array of review objects like this : "reviews": { "author": "5e9167c5303a530023bcae42", "rate": 5, "spoiler": false, "content": "This is a comment This is a comment This is a comment.", "createdAt": "2020-04-12T16:08:34.966Z", "updatedAt": "2020-04-12T16:08:34.966Z" }, What I want to achieve is to lookup the author field and get the user data, but the problem is that the lookup I am trying to use only returns this to me: Code : .lookup({ from: 'users', localField: 'reviews.author',