How to query a document which contains specific value inside it's property array?

1,469 views
Skip to first unread message

Jiří Špác

unread,
Mar 24, 2013, 3:59:08 PM3/24/13
to mongoo...@googlegroups.com
I have following schema:
var postSchema = mongoose.Schema({
    title: String,
    text: String,       //html
    createdAt: { type: Date, default: Date.now },
    tags: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Tag' }]
});

and I want to find all posts which have tag for example 514f0a738cb944c00f000001. How can I do that with Mongoose? 
Can I use $in for this?

Jiří Špác

unread,
Mar 24, 2013, 4:13:20 PM3/24/13
to mongoo...@googlegroups.com
Ok, I have figured it out already, here is the solution for future incoming googlers:
 Post.find({tags: {$in: ['514f0a738cb944c00f000001']}}).exec(function (err, posts) {
        console.log(posts);
    });



Dne neděle, 24. března 2013 20:59:08 UTC+1 Jiří Špác napsal(a):

Aaron Heckmann

unread,
Mar 27, 2013, 12:49:58 AM3/27/13
to mongoo...@googlegroups.com
MongoDB also allows querying arrays using the following, slightly simpler, syntax:

Post.find({ tags: '514f0a738cb944c00f000001' }, callback);

Generally, all of the MongoDB query syntax is supported through `Model.find(selector)`. 




--
--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
 
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron


Reply all
Reply to author
Forward
0 new messages