EthernetCable
EthernetCable

Reputation: 1327

Mongo count aggregation returns 0

I have a collection with a compound index for the attributes of an array. The query I'm running is:

db.servers.find({$query: { features: {$elemMatch: {'name':'a','status':'passed'}}}}).count()

This query used to return a valid number, but now it only returns 0. If I remove the count() command, I get a long list of matches.

The problem is similar to the one here- MongoDB's count() incorrectly returns 0 if a query is given

So far I've tried restarting, updating, running repaireDatabase, running reIndex, running dropIndexes and letting mongoose create the indexes for me, and running mongodump and mongorestore. I've been unable to run mongod with the --repair flag, since I'm using journaling.

There are hundreds of lines of this same warning in the log file-

warning: ClientCursor::yield can't unlock b/c of recursive lock ns: zrk.servers top: { opid: 26, active: true, secs_running: 9, op: "query", ns: "zrk", query: { repairDatabase: 1.0 }, client: "127.0.0.1:49917", desc: "conn1", threadId: "0x7f5dfb4c2700", connectionId: 1, locks: { ^: "W" }, waitingForLock: false, numYields: 0, lockStats: { timeLockedMicros: {}, timeAcquiringMicros: { R: 0, W: 3, r: 0, w: 2 } } }

I initially launched this mongod configured as a replica set, but I haven't yet added any secondary members. The data directory and the server are on separate EBS volumes.

EDIT: Thinking the problem was a corrupt database, I manually moved all of my documents to a fresh mongo. This solved the same problem in the above link, but didn't fix it for me.

I've also tried removing the index on the old, now empty, mongo and adding one or two documents to run the query on again. That didn't worked either. The non-id index is

{
                "v" : 1,
                "key" : {
                        "features.name" : 1,
                        "features.status" : 1,
                        "random" : 1
                },
                "ns" : "zrk.servers",
                "name" : "features.name_1_features.status_1_random_1",
                "background" : true,
                "safe" : null
        }

As you can see, I usually use this index to select random documents. But it is also used for the above find query.

Upvotes: 2

Views: 1207

Answers (1)

3rf
3rf

Reputation: 1164

I think the issue may be that you are using "$query" in your find query. Using "$query" as part of your find command is no longer necessary, and, according to the documentation, prevents cursor helpers from working. Try rewriting the query as something like

db.servers.find({features:{$elemMatch:{'name':'a','status':'passed'}}})

and then see if .count() works.

Upvotes: 4

Related Questions