Mongodb find()使用Node不返回所有文件(奇怪的行为)

时间:2017-08-11 16:23:02

标签: node.js mongodb meteor

我正在使用带有Node的Meteor来检索在mLab上托管的MongoDB集合中的车辆列表。我最近注意到我的js app中的find()没有返回集合中的所有匹配文档。使用Mongo shell并在mLab上搜索都会返回正确数量的结果。

这是集合中的示例文档,当它没有返回时  应该是:

{
    "_id": "VIN",
    "updatedOn": "Fri Aug 11 2017 11:27:40 GMT-0400 (EDT)",
    "clientId": "1001",
    "crushVersion": "v.3.42",
    "yardName": "YARD NAME",
    "yardCity": "CITY",
    "yardState": "STATE",
    "stockNumber": "STK123447",
    "iStatus": "0",
    "location": "YARD",
    "year": "2003",
    "make": "AUDI",
    "model": "A6",
    "vehicleRow": "32",
    "yardDate": "2017-08-10T18:09:38.363",
    "batchNumber": "STK123447",
    "lastUpdate": "08/11/2017 01:31:31 AM",
    "color": "SILVER",
    "vin": "VIN",
    "reference": "",
    "milage": "24"
}

......以及按预期返回的那个:

{
    "_id": "VIN",
    "updatedOn": "Fri Aug 11 2017 11:27:40 GMT-0400 (EDT)",
    "clientId": "1112",
    "crushVersion": "v.3.42",
    "yardName": "YARD NAME",
    "yardCity": "CITY",
    "yardState": "STATE",
    "stockNumber": "STK02752",
    "iStatus": "0",
    "location": "YARD",
    "year": "2003",
    "make": "AUDI",
    "model": "A6",
    "vehicleRow": "600",
    "yardDate": "2017-07-20T10:28:54.407",
    "batchNumber": "STK02752",
    "lastUpdate": "08/11/2017 08:30:30 AM",
    "color": "GRAY",
    "vin": "VIN",
    "reference": "",
    "milage": "1"
}

...和'find'的js代码;

MongoClient.connect(
    url,
    Meteor.bindEnvironment((err, db) => {
        if (err) throw new Meteor.Error(err)
        const inv = db.collection('inventory'),
            userId = user._id,
            picks = _picks, 
            /* _picks comes from a user-populated form as an array of objects. */
            makes = picks.map(pick => {
                return pick.make.toUpperCase()
            }),
            models = picks.map(pick => {
                return pick.model.toUpperCase()
            }),
            yards = user.profile.yards.map(yard => {
                return yard.yard
            })
        if (picks.length > 0) {
            inv
                .find({
                    $or: user.profile.picks.map(p => ({
                        year: { $gte: p.minYear, $lte: p.maxYear }
                    })),
                    make: { $in: makes },
                    model: { $in: models },
                    yardName: { $in: yards }
                })
                .toArray(
                    Meteor.bindEnvironment((err, docs) => {
                        if (err) throw new Meteor.Error(err)

                        console.log(docs)
                    })
                )
        }
    })
)

现在这是奇怪的部分。返回的所有文档都有yardDate2017-7-31或更早版本。这可能是巧合,但我不这么认为。我的集合有大约21000个文档,所有查询都表现出相同的行为。

我查看了这个问题Link,但我的一些查询只有一两个结果,所以我认为这不是缓冲区溢出问题。

感谢您提供任何帮助!

更新 我制作了一个小测试文件,看看我的find()链中是否有一些可能会弄乱查询的内容。这是代码:

const MongoClient = require('mongodb').MongoClient,
    url = 'mongodb://user:password@ipaddress/dbname'

MongoClient.connect(url, (err, db) => {
    const inv = db.collection('inventory')

    inv.find({ year: '2003', model: 'A6' }).toArray((err, docs) => {
        if (err) throw new Error(err)
        console.log(docs)
        db.close()
    })
})

这应该返回五个文档​​,但只返回两个...所有文件都yardDate早于2017-07-31

更新

我添加了“缺失”结果和“找到”结果。

0 个答案:

没有答案