架构是这样的:
var feedSchema = new mongoose.Schema({
id: {type: Number},
following: []
});
我使用promise的代码非常简单:
var Feed = require("/models/feed");
return Feed.find({"following" : id}).exec();
我有这样的数据:
{
id:1, following: [2,3,4]
}
但是当我为查询设置id时,它不会返回任何内容。任何的想法?
答案 0 :(得分:1)
它应该正常工作,我提供了一个工作正常的例子,可能你可以将你的逻辑与这个逻辑进行对比,并知道哪个部分可能是问题。
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 was not created')); }
cb(null, feed);
});
}
function findFeedByFollowingArray(id) {
return Feed
.find({following: id})
.exec();
}