如果引用的数组

时间:2017-05-13 08:43:34

标签: mongoose mongoose-schema

我的猫鼬模式如下:

foodTruckUserAttach.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const foodTruck = require('./foodtruck.js');
const foodTruckUser = require('./foodTruckUser.js');


const foodTruckUserAttached = new Schema({
    foodtruck_users: [{
        type: mongoose.Schema.ObjectId,
        ref: 'user',
        required: true
    }],
    foodtruck_id: {
        type: mongoose.Schema.ObjectId,
        ref: 'foodtruck',
        required: true
    }
}, {
    versionKey: false
});



module.exports = mongoose.model('foodtruckRegisteredUsers', foodTruckUserAttached);

现在我想要的是假设 foodTruckUserAttach 的特定文件具有 foodtruck_user ,其中包含ID 1,2,3。现在我想要的是,如果它具有id = 3的 foodtruck_user ,我想返回该特定文档。可以吗?

1 个答案:

答案 0 :(得分:1)

foodTruckUserAttached.find({foodtruck_users: 3}, function(err, ftua){
    if(!err){
        console.log(ftua)
    }
});