传统的猫鼬查询不适合我

时间:2017-12-21 12:20:16

标签: node.js mongoose

我正在制作这个mongoose查询以获取我的父文档,但一无所获。 大多数猫鼬的例子对我不起作用,有些人会发生同样的事情吗?

我的架构:

Parent var schema = new mongoose.Schema({
    name: String,
    email: String,
    phone: String,
    cell: String,
    properties: [{type: mongoose.Schema.Types.ObjectId, ref: 'Property'}]

}, {
        timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'} 
});
mongoose.model('Contact', schema); 


var schema = new mongoose.Schema({
        address: String,
        country: String,
        province: String,
        locality: String,
        partido: String,
        contracts: [{type: mongoose.Schema.Types.ObjectId, ref: 'Contract'}]
    }, {
        timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'} }); 

//查询

    var query = Contact.findOne({'propeties._id': req.params.id});
        query.populate('properties');
        query.exec(function (err, item) {
            if (err) return handleError(err);
            console.log(item);
        })

编辑!

我意识到,当我声明类似这个属性[Property]的模式关系时,它可以工作,但我需要使用这种方式属性:[{type:mongoose.Schema.Types.ObjectId,ref:' Property' }] 因为我只想要商店_id而不是所有文件。

0 个答案:

没有答案