使用包含ref对象数组填充Mongoose

fnvucqvd  于 2022-11-13  发布在  Go
关注(0)|答案(6)|浏览(150)

我有一个Mongoose模式,其中包含一个lists对象数组,该数组包含对另一个集合的引用和一个嵌套的数字数组:

var Schema, exports, mongoose, schema;
    
mongoose = require("mongoose");
    
Schema = mongoose.Schema;
    
schema = new Schema({
    name: {
        type: String,
        required: true,
        unique: true,
        trim: true
    },
    lists: [{
        list: {
            type: Schema.ObjectId,
            require: true,
            ref: "List"
        },
        allocations: [{
            type: Number,
            required: true
        }]
    }],
    createdAt: {
        type: Date,
        "default": Date.now
    },
    updatedAt: {
        type: Date
    }
});
    
exports = module.exports = mongoose.model("Portfolio", schema);

但是,如果没有TypeError: Cannot read property 'ref' of undefined,我就无法使populate按预期工作。我试过populate('list')populate('lists list'),但要么调用不正确,要么Schema的格式不正确。如果我只是引用列表本身,就不会有这个问题:

lists: [{
    type: Schema.ObjectId,
    require: true,
    ref: "List"
}]

但是我希望在每个列表旁边都有一个分配数组,那么我需要做什么来获得我想要的行为呢?

ioekq8ef

ioekq8ef1#

我找到了答案:populate('lists.list')工作。感谢这个问题:Mongoose populate within an object?

qlckcl4x

qlckcl4x2#

实际答案:

用这个,

populate('lists.list')

额外费用:

这是一个对象的数组(list)。在JS中你可以像这样访问它,

console.log(lists.list);

并且MongoDB语法与JS语法99%相似。因此.................

qkf9rpyu

qkf9rpyu3#

lists: [{
    list: {
        type: Schema.ObjectId,
        require: true,
        ref: "List"
    },
    allocations: [{
        type: Number,
        required: true
    }]
}],

=〉因为它是一个对象数组,所以你可以这样做-:Portfolio.populate('lists.list');
2.

lists: [{
    type: Schema.ObjectId,
    require: true,
    ref: "List"
}]

因为它是一个数组,你只需要这样做-:Portfolio.populate('lists');

im9ewurl

im9ewurl4#

如果您有嵌套架构

YourSchema.find()
   .populate({
        path: 'map_data',
        populate: {
            path: 'location' 
        }
})

对我很有效

dxxyhpgq

dxxyhpgq5#

// Cart schema  
var CartSchema = new mongooseSchema({
    productDetails: [{
        productId: {
            type: mongoose.Schema.ObjectId,
            required: true,
            ref: 'Product'

        },
        productCount: Number,
    }],
    UserId: {
        type: String,
        default: '',
        required: true,
        trim: true,
    },
    shopId: {
        type: String,
        default: '',
        required: true,
        trim: true,
    },
});

// add this .populate('productDetails.productId').

db.Cart.find({
    UserId: userId,
    shopId: shopId
}).populate('productDetails.productId')
.skip(pagination.skip)
.limit(pagination.limit)
.exec(function(error,

CartList) {

    if (error) {
        callback(error, null)
    } else {
        callback(null, CartList)
    }
});
gjmwrych

gjmwrych6#

在我的情况下,当嵌套到数组时,Populate不起作用,我的Schema是

const chatSchema = mongoose.Schema({
    chats: [{
        type: Schema.Types.ObjectId,
        ref: "ChatMsg" 
    }],
})

我是怎么解决的

Chat.findOne({ 
    customer: req.body.customer, 
    agency: req.body.agency 
}, (err, chat) => {
    if (err) {
        res.status(422).send("Our fault");
    }

    chat.populate("chats").execPopulate(() => {
        res.send(chat);
    });
});

相关问题