Mongoose没有返回完整的数据集

时间:2016-07-12 13:06:44

标签: node.js mongodb express mongoose

我的模型设置如下:

var Post = mongoose.Schema({

      "_uid": String,
      "post_title": String,
      "post_content": String,
      "post_date": Date,
      "user": String,
      "slug": String,
      "attached_media": [[
        {
          "format": String,
          "type": String,
          "small": String,
          "medium": String,
          "large": String
        }
      ]],
      "likes_count": Number,
      "likes": [
        {....

这是attach_media数组的关键部分,因为每当我使用带有mongoose的.find({})命令时,没有任何数组元素返回其中的任何数据。 数据肯定在其中,因为当我使用mongo命令shell时,我看到数组已填充。

我的发现方法:

Post.statics.getAll = function getAll(next){
  this.find({})
  .sort({'post_date':'desc'})
  .exec(function(err, doc){
    if(err) console.log(err)
    next(null, doc)
  })
}

猫鼬回归:

[ { attached_media: [],
    likes: [],
    comments: [],
    __v: 0,
    _uid: '577e67d2a4387d0b1b480e2c',
    user: 'Aaron Griffin',
    post_title: 'This should be a couple of images',
    post_date: Tue Jul 12 2016 13:40:06 GMT+0000 (UTC),
    post_content: '',
    _id: 5784f33681adc21c121a94d1 },
  { attached_media: [],
    likes: [],
    comments: [],
    __v: 0,
    _uid: '577e67d2a4387d0b1b480e2c',
    user: 'Aaron Griffin',
    post_title: 'Hello',
    post_date: Tue Jul 12 2016 13:34:03 GMT+0000 (UTC),
    post_content: '',
    _id: 5784f1cb81adc21c121a94d0 } ]

但是应该返回(mongo shell返回的内容):

{ "_id" : ObjectId("5784f33681adc21c121a94d1"), "post_content" : "", "post_date" : ISODate("2016-07-12T13:40:06.486Z"), "post_title" : "This should be a couple of images", "user" : "Aaron Griffin", "_uid" : "577e67d2a4387d0b1b480e2c", "comments" : [ ], "likes" : [ ], "attached_media" : [ { "large" : "large_f087dd83f697e3742d249e45d47f102b.jpg", "medium" : "medium_f087dd83f697e3742d249e45d47f102b.jpg", "small" : "small_f087dd83f697e3742d249e45d47f102b.jpg", "type" : ".jpg", "format" : "image/jpeg" }, { "large" : "large_fe6dfb7bfcff38883badb800d049dc19.jpg", "medium" : "medium_fe6dfb7bfcff38883badb800d049dc19.jpg", "small" : "small_fe6dfb7bfcff38883badb800d049dc19.jpg", "type" : ".jpg", "format" : "image/jpeg" }, { "large" : "large_527b0b9eb14f4310e535eae2c6e48f4a.jpg", "medium" : "medium_527b0b9eb14f4310e535eae2c6e48f4a.jpg", "small" : "small_527b0b9eb14f4310e535eae2c6e48f4a.jpg", "type" : ".jpg", "format" : "image/jpeg" } ], "__v" : 0 }
{ "_id" : ObjectId("5784f1cb81adc21c121a94d0"), "post_content" : "", "post_date" : ISODate("2016-07-12T13:34:03.848Z"), "post_title" : "Hello", "user" : "Aaron Griffin", "_uid" : "577e67d2a4387d0b1b480e2c", "comments" : [ ], "likes" : [ ], "attached_media" : [ { "large" : "large_c869348bfc5f12f4099a0f6e2d8941ae.jpg", "medium" : "medium_c869348bfc5f12f4099a0f6e2d8941ae.jpg", "small" : "small_c869348bfc5f12f4099a0f6e2d8941ae.jpg", "type" : ".jpg", "format" : "image/jpeg" } ], "__v" : 0 }

运行并记录查询的代码:

router.get('/', function(req, res, next) {
  if (req.user) {
    if(req.user.registration_level == 1){
      res.redirect('/signup/complete')
    }
    Post.getAll(function(err, posts){
      // console.log(req.user)
      console.log(posts)
      res.render('dashboard', { title: 'Member feed' , posts: posts, user: req.user });
    })
  } else {
      res.render('index.ejs', { title: 'Express' });
  }
});

如果有人知道为什么这不起作用我会感激任何帮助:)

2 个答案:

答案 0 :(得分:0)

我不确定你在那里使用了数组数组......你可以用这样的数组替换它。

var Post = mongoose.Schema({

  "_uid": String,
  "post_title": String,
  "post_content": String,
  "post_date": Date,
  "user": String,
  "slug": String,
  "attached_media": [
    {
      "format": String,
      "type": String,
      "small": String,
      "medium": String,
      "large": String
    }
  ],
  "likes_count": Number,
  "likes": [
    {....

答案 1 :(得分:0)

我明白了!

我需要使用Mongoose提供的子文档:

var mediaSchema = new mongoose.Schema({
  "format": String,
  "type": String,
  "small": String,
  "medium": String,
  "large": String
})

var Post = mongoose.Schema({
  "_uid": String,
  "post_title": String,
  "post_content": String,
  "post_date": Date,
  "user": String,
  "slug": String,
  "attached_media": [mediaSchema],
  "likes_count": Number,
  "likes": [
...

感谢大家的帮助:)