How to find the object by embedded document property in Mongoose?

3,065 views
Skip to first unread message

vabl...@gmail.com

unread,
Feb 3, 2015, 9:16:14 AM2/3/15
to mongoo...@googlegroups.com
Hello,
I have created 2 schemas like this

var LinkSchema = new mongoose.Schema({
    url
: String,
    name
: String
});
var Link = mongoose.model('Link', LinkSchema);

var UserSchema = new mongoose.Schema({
    name
: String,
    links
: [LinkSchema]
});
var User = mongoose.model('User', UserSchema);

Then I would like to find the user object that has link too google.com
User.find({'links.url': 'http://google.com' },function(err, foundLinks){
    console
.log(foundLinks);
});

but the empty list was returned
[]

Could anyone suggest me how can I solve it?

Here is full source code:
var mongoose = require('mongoose');
var async = require('async');
var Schema = mongoose.Schema;

mongoose
.connect('mongodb://localhost/test');

var LinkSchema = new mongoose.Schema({
    url
: String,
    name
: String
});
var Link = mongoose.model('Link', LinkSchema);

var UserSchema = new mongoose.Schema({
    name
: String,
    links
: [LinkSchema]
});
var User = mongoose.model('User', UserSchema);

var link = new Link();
link
.url = "http://google.com";
link
.name = "Google";
link
.save(function(err,link){
   
//console.log(link);
   
var user = new User();
    user
.name = "Varavut";
    user
.links.push(link._id);
    user
.save(function(err,user){
       
//console.log(user);
       
User.find({'links.url': 'http://google.com' },function(err, foundLinks){
               console
.log(foundLinks);
           
});
   
});
});


Thank you very much.
ps. Sorry for my bad English.

Jason Crawford

unread,
Feb 3, 2015, 10:44:09 AM2/3/15
to mongoo...@googlegroups.com
You are trying to do a join—looking up a model in one collection based on attributes of models it references in another collection. You can't do that in Mongo.

In fact, the way you've set up your schemas here is inconsistent. You've defined Link as a model on its own, but then you've also nested the LinkSchema in the User schema.

You have to choose: either Links are a separate collection, or they're nested documents under Users. If they are nested, then you can do the query you're trying to do. If they are not, then you have to do this in two steps: first look up the Link objects with the given url, then look up the Users referencing those Link ids.

Hope that helps,
Jason

--
Blog: http://blog.jasoncrawford.org  |  Twitter: @jasoncrawford



--
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 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/d/optout.

vabl...@gmail.com

unread,
Feb 3, 2015, 7:10:45 PM2/3/15
to mongoo...@googlegroups.com
Thank you very much.

Now, I go with query an id of link first then find for the user by an id of link.
Reply all
Reply to author
Forward
0 new messages