How can I search an embedded collection within MongoDB?

By : Hoa
Source: Stackoverflow.com
Question!

For example suppose I have a Post schema as follows:

var Schema = new db.Schema({
    title    : String
    , text    : String

    // Denormalize by using user name
    , author     : String
    , tags: [String]
    , comments: [Comment]
});

I'm trying to match a particular tag as follows:

function getPostByTag(tag, callback) {
    Post.find().where({"tags.value": tag}).run(callback);
}

But it is returning everything. What am I doing wrong?

By : Hoa


Answers

Where are you getting the "value" part of "tags.value". Shouldn't it just be:

Post.find().where({tags:tag}).run(callback);


This video can help you solving your question :)
By: admin