Created
May 14, 2013 19:00
-
-
Save jmyrland/5578516 to your computer and use it in GitHub Desktop.
Mongoose: Testing query for embedded docs - http://stackoverflow.com/questions/16528539/mongoose-query-for-an-array-of-dictionaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Related to http://stackoverflow.com/questions/16528539/mongoose-query-for-an-array-of-dictionaries | |
*/ | |
var mongoose = require('mongoose'), | |
db = mongoose.connect('mongodb://localhost/test'); | |
var blogSchema = new mongoose.Schema({ | |
group: String, | |
writing: [{ | |
post: String, | |
name : Number | |
}] | |
}); | |
var Blog = mongoose.model('Blog', blogSchema); | |
var blogData = { | |
group: "Design Writing", | |
writing: [{ | |
post: "A very very very short post." + Math.random(), | |
name: 10 | |
}, { | |
_id: new mongoose.Types.ObjectId(), | |
post: "An early morning post about everything.", | |
name: 11 | |
}] | |
}; | |
var postObject = blogData.writing[0], | |
postObjectWithId = blogData.writing[1]; | |
Blog.create(blogData, function(err){ | |
console.log(err ? err : 'Blog saved successfully!'); | |
// Example #1 Works! | |
Blog.find({'writing.post': postObject.post}) | |
.exec(function(err, res){ onBlogResult("Ex#1", err, res) }); | |
// Example #2 Works! | |
Blog.find({}) | |
.where('writing').in([postObjectWithId]) | |
.exec(function(err, res){ onBlogResult("Ex#2", err, res) }); | |
// Example #3 Works - its the same as example #2! | |
Blog.find({'writing': { $in: [ postObjectWithId ]}}) | |
.exec(function(err, res){ onBlogResult("Ex#3", err, res) }); | |
// Example #4 Fails because of missing _id on postObject! | |
Blog.find({'writing': { $in: [ postObject ]}}) | |
.exec(function(err, res){ onBlogResult("Ex#4", err, res) }); | |
}) | |
function onBlogResult(ex, err, result){ | |
if(err || result.length == 0) | |
return console.log(ex, " - No matching document or Error"); | |
console.log(ex, " - Found blog: ", result); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Blog saved successfully! | |
Ex#1 - Found blog: [ { group: 'Design Writing', | |
_id: 519288e1ceba8bc97e000003, | |
__v: 0, | |
writing: | |
[ { post: 'A very very very short post.0.8866961947642267', | |
name: 10, | |
_id: 519288e1ceba8bc97e000004 }, | |
{ _id: 519288e1ceba8bc97e000002, | |
post: 'An early morning post about everything.', | |
name: 11 } ] } ] | |
Ex#2 - Found blog: [ { group: 'Design Writing', | |
_id: 519288e1ceba8bc97e000003, | |
__v: 0, | |
writing: | |
[ { post: 'A very very very short post.0.8866961947642267', | |
name: 10, | |
_id: 519288e1ceba8bc97e000004 }, | |
{ _id: 519288e1ceba8bc97e000002, | |
post: 'An early morning post about everything.', | |
name: 11 } ] } ] | |
Ex#3 - Found blog: [ { group: 'Design Writing', | |
_id: 519288e1ceba8bc97e000003, | |
__v: 0, | |
writing: | |
[ { post: 'A very very very short post.0.8866961947642267', | |
name: 10, | |
_id: 519288e1ceba8bc97e000004 }, | |
{ _id: 519288e1ceba8bc97e000002, | |
post: 'An early morning post about everything.', | |
name: 11 } ] } ] | |
Ex#4 - No matching document or Error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment