首页 文章

mongoose#populate在数组内的嵌套对象中返回null

提问于
浏览
1

我有一个mongoDB数据库,它是使用一个脚本生成的,该脚本只使用没有mongoose的node.js mongoDB驱动程序 . 稍后,在应用程序中,我想使用mongoose加载文档并自动填充引用;然而,这只会返回 null .

想象一个包含子项的任务,每个子项都有一个 Headers 和一个指定的人 . 在这种情况下,分配的人是我想要填充的引用,因此引用存在于任务模式中的数组内的对象中 .

以下代码(需要 npm install mongodb mongoose )重现问题(注意,它会破坏名为 test 的本地数据库,如果您已经存在):

const mongodb = require('mongodb');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

(async () => {
    // Step 1: Insert data. This is done using the mongodb driver without mongoose.
    const db = await mongodb.MongoClient.connect('mongodb://localhost/test');
    await db.dropDatabase();
    await db.collection('persons').insertOne({ name: 'Joe' });

    const joe = await db.collection('persons').findOne({ name: 'Joe' });
    await db.collection('tasks').insertOne({ items: [{ title: 'Test', person: joe._id }] });

    await db.close();

    // ================
    // Step 2: Create the schemas and models.
    const PersonSchema = new Schema({
        name: String,
    });
    const Person = mongoose.model('Person', PersonSchema);

    const TaskSchema = new Schema({
        items: [{
            title: String,
            person: { type: Schema.Types.ObjectId, ref: 'Person' },
        }],
    });
    const Task = mongoose.model('Task', TaskSchema);

    // ================
    // Step 3: Try to query the task and have it populated.
    mongoose.connect('mongodb://localhost/test');
    mongoose.Promise = Promise;

    const myTask = await Task.findOne({}).populate('items.person');

    // :-( Unfortunately this prints only
    // { _id: "594283a5957e327d4896d135", items: [ { title: 'Test', person: null } ] }
    console.log(JSON.stringify(myTask, null, 4));

    mongoose.connection.close();
})();

预期的产出是

{ _id: "594283a5957e327d4896d135", items: [ { title: 'Test', person: { _id: "594283a5957e327d4896d134", name: "Joe" } } ] }

我已经验证了两个 _id 实际上使用mongo shell匹配:

> db.persons.find({})
{ "_id" : ObjectId("594283a5957e327d4896d134"), "name" : "Joe" }
> db.tasks.find({})
{ "_id" : ObjectId("594283a5957e327d4896d135"), "items" : [ { "title" : "Test", "person" : ObjectId("594283a5957e327d4896d134") } ] }

尝试填充 person 时我做错了什么?我使用的是mongoose 4.10.6和mongodb 2.2.28 .

1 回答

  • 0

    这个问题的答案在于,集合名称mongoose自动从模型 Person 推断出 people 而不是 persons .

    可以通过在第一部分中写入 people 集合或强制mongoose使用集合名称 persons 来解决此问题:

    const Person = mongoose.model('Person', PersonSchema, 'persons');
    

    mongoose计划删除集合名称中的复数,请参阅Github上的#1350 .

相关问题