猫鼬:填充一个已填充的字段 [英] Mongoose: Populate a populated field

查看:108
本文介绍了猫鼬:填充一个已填充的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将MongoDB用作应用程序的日志管理器,然后同步移动客户端.我在NodeJS中设置了以下模型:

I'm using MongoDB as a log keeper for my app to then sync mobile clients. I have this models set up in NodeJS:

var UserArticle = new Schema({
    date: { type: Number, default: Math.round((new Date()).getTime() / 1000) }, //Timestamp!
    user: [{type: Schema.ObjectId, ref: "User"}],
    article: [{type: Schema.ObjectId, ref: "Article"}],
    place: Number,    
    read: Number,     
    starred: Number,   
    source: String
});
mongoose.model("UserArticle",UserArticle);

var Log = new Schema({
    user: [{type: Schema.ObjectId, ref: "User"}],
    action: Number, // O => Insert, 1 => Update, 2 => Delete
    uarticle: [{type: Schema.ObjectId, ref: "UserArticle"}],
    timestamp: { type: Number, default: Math.round((new Date()).getTime() / 1000) }
});
mongoose.model("Log",Log);

当我想检索日志时,请使用以下代码:

When I want to retrive the log I use the follwing code:


var log = mongoose.model('Log');
log
.where("user", req.session.user)
.desc("timestamp")
.populate("uarticle")
.populate("uarticle.article")
.run(function (err, articles) {
if (err) {
    console.log(err);
        res.send(500);
    return;
}
res.json(articles);

如您所见,我希望猫鼬填充Log集合中的"uarticle"字段,然后再填充UserArticle("uarticle")的"article"字段.

As you can see, I want mongoose to populate the "uarticle" field from the Log collection and, then, I want to populate the "article" field of the UserArticle ("uarticle").

但是,使用此代码,Mongoose仅使用UserArticle模型填充"uarticle",而不填充uarticle内部的article字段.

But, using this code, Mongoose only populates "uarticle" using the UserArticle Model, but not the article field inside of uarticle.

是否可以使用Mongoose和populate()完成它,或者我应该做其他事情?

Is it possible to accomplish it using Mongoose and populate() or I should do something else?

谢谢

推荐答案

根据我在文档中所检查的内容以及从您那里得到的信息,这是无法实现的,但是您可以填充"uarticle.article"文档自己使用回调函数.

From what I've checked in the documentation and from what I hear from you, this cannot be achieved, but you can populate the "uarticle.article" documents yourself in the callback function.

但是,我想指出另一个我认为更重要的方面.在集合A中有引用集合B的文档,而在集合B的文档中,您还有对集合C的文档的引用.

However I want to point out another aspect which I consider more important. You have documents in collection A which reference collection B, and in collection B's documents you have another reference to documents in collection C.

您可能做错了(我指的是数据库结构),或者您应该在此处使用诸如MySQL之类的关系数据库. MongoDB的强大功能在于您可以在文档中嵌入更多信息,从而不必进行更少的查询(将数据存储在一个集合中).虽然可以引用某些东西,但是先引用然后再引用似乎并不意味着您在这里充分利用了MongoDB.

You are either doing this wrong (I'm referring to the database structure), or you should be using a relational database such as MySQL here. MongoDB's power relies in the fact you can embed more information in documents, thus having to make lesser queries (having your data in a single collection). While referencing something is ok, having a reference and then another reference doesn't seem like you're taking the full advantage of MongoDB here.

也许您想分享您的情况和数据库结构,以便我们为您提供更多帮助.

Perhaps you would like to share your situation and the database structure so we could help you out more.

这篇关于猫鼬:填充一个已填充的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆