node.js - How come Mongoose return empty document when find by id of an array? -
the schema that:
var feedschema = new mongoose.schema({ id: {type: number}, following: [] });
and code using promise simple:
var feed = require("/models/feed"); return feed.find({"following" : id}).exec();
i have data this:
{ id:1, following: [2,3,4] }
but when set id query, doesn't return anything. idea?
it should working, providing example works ok, can contrast logic 1 , know part problem.
var mongoose = require('mongoose'); var schema = mongoose.schema; mongoose.connect('mongodb://localhost:27017/murvinlai'); var feedschema = new schema({ id: number, following: [number] }); var feed = mongoose.model('feed', feedschema); var newfeed = { id: 1, following: [2, 3, 4] }; createfeed(newfeed, function(err, feed) { if (err) throw err; findfeedbyfollowingarray(2).then(function(feeds) { console.log(feeds); // [ { _id: 55a486a11ef682b41e13e82a, // id: 1, // __v: 0, // following: [ 2, 3, 4 ] } ] }); }); function createfeed(feed, cb) { feed.create(feed, function(err, feed) { if (err) { return cb(err); } if (!feed) { return cb(new error('feed not created')); } cb(null, feed); }); } function findfeedbyfollowingarray(id) { return feed .find({following: id}) .exec(); }
Comments
Post a Comment