将mongo嵌套数组查询转换为mongoose

时间:2019-07-26 13:37:54

标签: node.js mongodb mongoose

我在mongo中使用以下查询,它返回了预期的结果

db.getCollection('trainings').find({"sections.employees.employee":ObjectId("5d3afa1a58a7160ea451d1db")})

但是当我尝试用猫鼬进行以下调用时,它将返回一个空数组

Training.find({'section.employees.employee': "5d3afa1a58a7160ea451d1db"})

需要一些帮助将mongo查询转换为mongoose。

如果您还需要其他任何内容,请添加评论,我将在此处发布。

值得一提的是,我已经尝试过传递带有String值的新ObjectId,但它还会返回一个空数组。

编辑:查询使用情况

        return await Training.find({'section.employees.employee': "5d3afa1a58a7160ea451d1db"})

编辑:模型

const mongoose = require("mongoose");
const Schema = mongoose.Schema;


const EmployeeSection = new Schema({
    employee: {type: Schema.Types.ObjectId, ref: 'Employee', required: true},
    fulfilled: {type: Boolean, default: null},
});

const Section = new Schema({
    order: {type: Number, required: true},
    date: {type: Date},
    employees: {type: [EmployeeSection]},
    answers: {type: Number},
    hits: {type: Number}
});

const GoalSchema = new Schema({
    understanding: {type: Number, required: true},
    fixation: {type: Number, required: true}
});


const Evidence = new Schema({
    description: {type: String, required: true},
    file: {type: String}
});

const Question = new Schema({
    participants: {type: Number},
    answers: {type: Number},
    hits: {type: Number}
});

const TrainingSchema = new Schema({
    company: {type: Schema.Types.ObjectId, ref: 'Company', required: true, index: true},
    creationDate: { type: Date, default: Date.now },
    title: {type: String},
    obs: {type: String},
    questionNumber: {type: Number},
    reference: {type: String},
    sections: {type: [Section]},
    goals: {
        answers: {type: GoalSchema, required: true},
        hits: {type: GoalSchema, required: true}
    },
    evidences: {type: [Evidence]},
    questions: {type: [Question]},
    area: {type: Schema.Types.ObjectId, ref: 'TrainingArea', required: true},
});


const Training = mongoose.model('Training', TrainingSchema);


module.exports = Training;

如果问题确实存在,但我不想这样做,我愿意更改Model模式。

2 个答案:

答案 0 :(得分:0)

为什么总是错字?

db.getCollection('trainings').find({"sections.employees.employee":ObjectId("5d3afa1a58a7160ea451d1db")})


Training.find({'section.employees.employee': "5d3afa1a58a7160ea451d1db"})

忘记了部分内容

答案 1 :(得分:0)

让这个问题对我来说容易一些。

const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/training", { useNewUrlParser: true })
const util = require("util")
const Schema = mongoose.Schema;


const EmployeeSection = new Schema({
    employee: {type: Schema.Types.ObjectId, ref: 'Employee', required: true}
});

const Section = new Schema({
    employees: {type: [EmployeeSection]}
});

const TrainingSchema = new Schema({
    sections: {type: [Section]}
});


const Training = mongoose.model('Training', TrainingSchema);



Training.find(
    {
            "sections.employees.employee": mongoose.Types.ObjectId("5d3afa1a58a7160ea451d1db")
    })
    .exec()
    .then(result => {
        console.log(util.inspect(result, false, null, true /* enable colors */))
    }).catch(err =>{
        console.log(err)
    })