简体   繁体   English

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

[英]Mongodb find() using Node not returning all documents (strange behavior)

I'm using Meteor with Node to retrieve a list of vehicles from a MongoDB collection, hosted on mLab. 我正在将Meteor与Node结合使用,以从在mLab上托管的MongoDB集合中检索车辆列表。 I recently noticed that find() in my js app was not returning all the matching documents in the collection. 我最近注意到,我的js应用程序中的find()并未返回集合中所有匹配的文档。 Using Mongo shell and the search on mLab both return the correct number of results. 使用Mongo Shell和在mLab上进行搜索均会返回正确数量的结果。

Here's a sample document from the collection that isn't returned when it should be: 以下是该集合中的示例文档,该文档应在应返回时不返回:

{
    "_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"
}

...and one that's returned as expected: ...并按预期返回:

{
    "_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"
}

...and the js code for 'find'; ...以及“ 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)
                    })
                )
        }
    })
)

Now here's the weird part. 现在这是奇怪的部分。 All the documents returned have a yardDate of 2017-7-31 or older. 返回的所有文件的yardDate2017-7-31或更早。 This may be a coincidence, but I don't think so. 这可能是一个巧合,但我不这么认为。 My collection has around 21000 documents and all queries exhibit the same behavior. 我的收藏大约有21000个文档,所有查询都表现出相同的行为。

I looked at this question Link , but some of my queries only have one or two results, so I don't think it's a buffer overflow issue. 我看着这个问题Link ,但是我的一些查询只有一个或两个结果,所以我认为这不是缓冲区溢出的问题。

Thanks for any help you can give! 谢谢你提供的所有帮助!

Update: I made a little test file to see if there's something up the chain from my find() that may be messing with the query. 更新:我做了一个小测试文件,以查看我的find()链中是否有某些内容可能会干扰查询。 Here's the code for that: 这是该代码:

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()
    })
})

This should return five documents, but only returns two... all with a yardDate older than 2017-07-31 . 这应该返回五个文档​​,但是仅返回两个...都具有yardDate早于2017-07-31

Update: 更新:

I added a 'missing' result and a 'found' result. 我添加了“丢失”结果和“找到”结果。

Turns out... when I moved my database to mLab, I didn't find all the urls in the app and change them. 原来,当我将数据库移至mLab时,我没有找到应用程序中的所有URL并进行更改。 The original database was still talking, so I was getting the 'old' results. 原始数据库仍在讨论中,因此我得到了“旧”结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM