0

So I have this field contacts.envCon.name which is inside the Projects collection but when I see them in mongo they are like this:

    "envCon" : {
        "$ref" : "contacts",
        "$id" : ObjectId("5807966090c01f4174cb1714")
    }

After doing a simple find based on past ObjectId:

db.getCollection('contacts').find({_id:ObjectId("5807966090c01f4174cb1714")})

I get the following result:

{
    "_id" : ObjectId("5807966090c01f4174cb1714"),
    "name" : "Terracon"
}

By the way: I'm using Meteor if there is anyway to do this directly with publish/suscribe methods.

4

1 回答 1

0

是的,您可以使用非常流行的reywood:publish-composite包在出版物中执行此连接。

使用您的模型:

Meteor.publishComposite('projectsWithContacts', {
    find: function() {
        return Projects.find(); // all projects
    },
    children: [
        {
            find: function(p) { // p is one project document
                return Contacts.find(
                    { _id: p.envCon.$id }, // this is the relationship
                    { fields: { name: 1 } }); // only return the name (_id is automatic)
            }
        },
    ]
});
于 2016-10-19T23:46:02.237 回答