具有populate()和limit选项的猫鼬find()查询未按预期返回结果

时间:2019-04-25 22:08:51

标签: mongoose

我必须对甲板和卡片进行建模。卡中包含“ deck”的引用以创建关系。我已经在每个卡座下设置了一个虚拟阵列,以将所有匹配的卡作为阵列获取。

通常情况下,一切都会按预期进行,我可以在卡片组中填充卡片。

但是,如果我传入一个选项来填充以限制数组中返回的卡的数量,则会得到意外的结果。具体来说,有些牌组会返回卡,有些则不会。

当我传递“ sort”选项进行填充时,出于某种原因,我开始获得某些套牌的卡,但不是全部。

我的架构定义:

const mongoose = require('mongoose')

var CardSchema = new mongoose.Schema({
    question: {
      type: String,
      required: true
    },
    answer: {
      type: String,
      required: true
    },
    deck: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Deck',
      required: true
    },
    created_at: {
        type: Date,
        default: Date.now(),
        index: true
    },
    active: {
        type: Boolean,
        required: false
    }
  })

var Card = mongoose.model('Card', CardSchema)

var DeckSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: false
  },
  userId: {
    type: String,
    required: true 
  },
  username: {
    type: String
  },
  created_at: {
      type: Date,
      default: Date.now(),
      index: true
  },
  active: {
      type: Boolean,
      required: false
  }
},{
  toObject: {
    virtuals: true
  },
  toJSON: {
    virtuals: true
  }
})

DeckSchema.virtual('cards', {
  ref: 'Card',
  localField: '_id',
  foreignField: 'deck'
})

var Deck = mongoose.model('Deck', DeckSchema)

module.exports = {
    Deck: Deck,
    Card: Card
}

示例卡和卡片组文件:

卡:

{
    "_id": {
        "$oid": "5cb6561a17055781fa83a401"
    },
    "created_at": {
        "$date": "2019-04-16T22:24:26.934Z"
    },
    "question": "Brunei",
    "answer": "Bandar Seri Begawan",
    "deck": {
        "$oid": "5ca09a141365a500179dbdd9"
    },
    "__v": 0
}

甲板:

{
    "_id": {
        "$oid": "5ca09a141365a500179dbdd9"
    },
    "created_at": {
        "$date": "2019-03-31T10:41:18.640Z"
    },
    "name": "Capitals of the World",
    "userId": "5c97d9147295f32991fd2606",
    "__v": 49
}

我的nodejs代码可使用3张卡片提取卡组:

router.get('/', checkIfTokenBelongsToUser ,(req, res) => {
    Deck.find({userId: req.params.userId})
        .populate({
            path: 'cards',
            select: 'card question',
            options: {
                sort: 'question',
                limit: 3,
            }
        })
        .exec((err, decks) => {
        if (err) {
            res.status(500).json({error: err})
        }
        else if(!decks.length) {
            console.log('No decks for this user')
            res.status(200).json(decks)
        }
        else {
            console.log('Fetched decks')
            console.log(decks.length)
            res.status(200).json(decks)
        }
    })
})

Mongodb调试输出:

Mongoose: decks.find({ userId: '5c97d9147295f32991fd2606' }, { projection: {} })
Mongoose: cards.find({ deck: { '$in': [ ObjectId("5cb65771ca969f83411413ec"), ObjectId("5cb65f75b8237f06922308e8"), ObjectId("5cbc8cee8d5f8113e9fea60e"), ObjectId("5cbcd1fc672a8e22e486808b"), ObjectId("5cbcd229e638eb23101c5711"), ObjectId("5cbcd24284841423430719df"), ObjectId("5cbcdaae69adad2cf25a9f37"), ObjectId("5cbcdc25ee67b62ede0e5d08"), ObjectId("5cbce1402e8d1634d49df744"), ObjectId("5cbce1b308d0483574fee3b0"), ObjectId("5cbce1db8315c935b351dcb9"), ObjectId("5cbce1f96054e735e34ca461"), ObjectId("5cbce22944ba9136059f3fcc"), ObjectId("5ca09a141365a500179dbdd9"), ObjectId("5ca9c095fb968c0017c5d531"), ObjectId("5c9f8ba7d2e7d20017595dc3") ] } }, { sort: { question: 1 }, limit: 48, projection: { card: 1, question: 1, deck: 1 } })

JSON响应:

[
    {
        "created_at": "2019-04-16T22:29:38.658Z",
        "_id": "5cb65771ca969f83411413ec",
        "name": "Countries by Continent",
        "userId": "5c97d9147295f32991fd2606",
        "__v": 0,
        "cards": [],
        "id": "5cb65771ca969f83411413ec"
    },
    {
        "created_at": "2019-04-16T23:04:19.542Z",
        "_id": "5cb65f75b8237f06922308e8",
        "name": "Jeopardy",
        "userId": "5c97d9147295f32991fd2606",
        "__v": 0,
        "cards": [
            {
                "_id": "5cb65f8de9cab80718476e8c",
                "question": "'\"'...Why look'st thou so?'--'With my crossbow I shot the albatross'\"'",
                "deck": "5cb65f75b8237f06922308e8"
            },
            {
                "_id": "5cb65f8de9cab80718476e80",
                "question": "'\"'Do all lawyers defend n-negroes, Atticus?'  'Of course they do, Scout'\"'",
                "deck": "5cb65f75b8237f06922308e8"
            },
            {
                "_id": "5cb65f8de9cab80718476eb5",
                "question": "'\"American Idiot\",<br />\"Dookie\"'",
                "deck": "5cb65f75b8237f06922308e8"
            }
        ],
        "id": "5cb65f75b8237f06922308e8"
    }
....

0 个答案:

没有答案