Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What I am trying to do is the following.

I have objects that could live in multiple documents, and although I am not sure if this is best practice, I am using their ObjectId as pointers.

Document A

{
"_id": {
    "$oid": "51a02dade4b02780aeee5ab7"
},
"title": "My Document A",
"artifacts": [
    "51a81d8ee4b084336fea2d33",
    "asdfsdfe4b084336fea2d33"
]

}

Document B

{
"_id": {
    "$oid": "51a02dade4b02780aeee5ab7"
},
"title": "My Document A",
"artifacts": [
    "51a81d8ee4b084336fea2d33",
    "123454b084336fea2d33"
]

}

The individual artifact looks something like. If this artifact changes then there is no need to update the documents that it lives in:

{
    "_id": {
        "$oid": "51a81d8ee4b084336fea2d33"
    },
    "title": "Artifact A",
    "media": {
        "player": "video",
        "source": "http://www.youtube.com/watch?v=12334456"
    }
}

What I'd like to do, is get an expanded list of all the artifacts shown in either doc A or doc B in an array something like:

[{
        "_id": {
            "$oid": "51a81d8ee4b084336fea2d33"
        },
        "title": "Artifact A",
        "media": {
            "player": "video",
            "source": "http://www.youtube.com/watch?v=12334456"
        }
    },
{
        "_id": {
            "$oid": "123455e4b084336fea2d33"
        },
        "title": "Artifact B",
        "media": {
            "player": "video",
            "source": "http://www.youtube.com/watch?v=12334456"
        }
    }]

The way I have done it in the past is by POSTing from client a list of Ids and then using

{_id: { $in: userQuestArray} } 

but if i am constantly posting back to node from the client it seems a bit inefficient. Especially if the array I am posting is hundreds of items long.

share|improve this question
I'm not exactly sure what your question is. MongoDb doesn't have functionality to return "linked" documents in a single query. Your technique is a common solution. Some language drivers have a way of making it seem like it does, but it's only an illusion of something more efficient. – WiredPrairie 5 hours ago
I think the mongoose populate feature might solve your problem. It provides functions in mongoose to link documents, using mongoose Schema, and tools that help update/query using those relationships between documents. mongoosejs.com/docs/populate.html – Matt Bakaitis 4 hours ago

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.