Last Updated
Viewed 17 Times

I Need to query both of these schemas with one db call (to improve performance)

I have two lists of _ids.

One list contains product_ids and others I have shop_ids.

Product Schema:

var productSchema = new Schema({
    productId: { type: String, index: { unique: true } },
    shopId: { type: String },
    merchantId: { type: String },
    title: { type: String },
    quantity: { type: String },
    outOfStock: {
        type: Boolean
    ...
    }
})

Shop Schema:

var shopSchema = new Schema({
    shopId: { type: 'String', index: { unique: true } },
    merchantId: {
        type: 'String'
    },
    marketeerId: {
        type: 'String'
    },
    LatLong: {
        latitude: {
            type: 'String'
        },
        longitude: {
            type: 'String'
        }
    ...
    }
})

Normally I use

shopModel.find(_id: { $in: [shop_ids] }, function (err, shops) {
    productModel.find(_id: {
        $in: [product_ids], function(err, products) {

        }
    })
})

is there any way to find all matching docs with these two schemas with one db call? without having two find queries?

I have 2 collections one is for group details and other is for group_members details, group_members have group's _id stored (as FK)

Like group : -> _id,name,type,date

group_members -> _id,group_id,members_id,role,status

so how can I get the groups that have most count of memebrs in that with order by group_membrs's count (top 10 groups with more members)

I have two Mongoose model schemas setup so that the child documents reference the parent documents, as opposed to the Parent documents having an array of Children documents. (Its like this due to the 16MB size limit restriction on documents, I didnt want to limit the amount of relationships between Parent/Child docs):

// Parent Model Schema
const parentSchema = new Schema({
    name: Schema.Types.String
})


// Child Model Schema
const childSchema = new Schema({
    name: Schema.Types.String,
    _partition: {
        type: Schema.Types.ObjectId,
        ref: 'Parent'
    }
})

I want to create a static method that I can query for a Parent document, then query for any Child documents that match the parent document, then create a new item in the parent document that will reference the Children array.

Basically if the Parent document is:

{
    _id: ObjectId('56ba258a98f0767514d0ee0b'),
    name: 'Foo'
}

And the child documents are:

[
    {
        _id: ObjectId('56b9b6a86ea3a0d012bdd062'),
        name: 'Name A',
        _partition: ObjectId('56ba258a98f0767514d0ee0b')
    },{
        _id: ObjectId('56ba7e9820accb40239baedf'),
        name: 'Name B',
        _partition: ObjectId('56ba258a98f0767514d0ee0b')
    }
]

Then id be looking to have something like:

{
    _id: ObjectId('56ba258a98f0767514d0ee0b'),
    name: 'Foo',
    children: [
        {
            _id: ObjectId('56b9b6a86ea3a0d012bdd062'),
            name: 'Name A',
            _partition: ObjectId('56ba258a98f0767514d0ee0b')
        },{
            _id: ObjectId('56ba7e9820accb40239baedf'),
            name: 'Name B',
            _partition: ObjectId('56ba258a98f0767514d0ee0b')
        }
    ]
}

Also, I want them to remain Mongoose documents, so I can update the Parents and Assets if I need to.

I was able to accomplish this by using toJSON on the Parent, then creating a new item that would hold the Child docs, but then obviously the Parent document isn't a real document..

The error I kept getting when I tried this was that I couldnt create a new element in the Document that wasnt in the schema.

I know I could do something like create a Virtual item that would return the promise that would query the Children, but im looking to have one static method, that returns one response (meaning they dont have to handle the virtual item as a promise or callback)

Let me know if this is possible. Thanks!

There are two Schemas: User Schema and Book schema. There are n users and can have any number of books. I now have to fetch all the books existing in the database. I'm unable to sort out any solution for that. I have tried using populate but it isn't working. What can be the best approach for this kind problem. I dont want any data of User Schema, only bookList of every user.

Here is the userSchema.js

var mongoose = require('mongoose');
var Book = require('./book');

var userSchema={
    profile:{
        // Some more fields.
    },
    bookList:[Book.bookSchema] //Interested here
};

module.exports = new mongoose.Schema(userSchema);
module.exports.userSchema = userSchema;

Here is bookSchema

var mongoose = require('mongoose');

var bookSchema = {
    __id : mongoose.Schema.Types.ObjectId,
    info:{
        //Some fields.
    }
}

module.exports = new mongoose.Schema(bookSchema);
module.exports.bookSchema = bookSchema;

The model.

var mongoose = require('mongoose');
var _ = require('underscore');
var autoIncrement = require('mongoose-auto-increment');
module.exports = function(wagner){
//Some codes
    var Book = mongoose.model('Book', require('./book'), 'books');
    var User = mongoose.model('User', require('./user'), 'users');
    var models = {
            Book:Book,
            User:User
    }
    _.each(models, function(value, key) {
        wagner.factory(key, function() {
          return value;
        });
    });
    return models;
}

Can I get any help in here? I've been stuck for hours!

Similar Question 6 (2 solutions) : Mongoose MongoDB Objects undefined

Similar Question 7 (1 solutions) : Relationships between schema in mongoose and mongoDB

Similar Question 9 (1 solutions) : Data structure for movie ticketing system in MongoDB

cc