How can I clear the mongoDB oplog?

Can MongoDB index for the "$exists" query?

  • In some data mining scenarios, I have a MongoDB collection whose records are heterogenous (i.e. they can have different keys). I often query for "which records have a field with a given key", as in db.coll.find({"fieldname":{$exists:1}}). I am wondering if there is a way to tell MongoDB to index for this type of query? It seems that all indexing in MongoDB is for values rather than keys. That said, I know that I could build a separate table whose values are all available keys and do queries there. I am wondering if there is a direct way to index keys in MongoDB.

  • Answer:

    In MongoDB 1.8 $exists doesn't use indexes. In 1.9 it does[1]. 1.9 is the unstable development release so this will be in the next stable i.e. 2.0. [1] http://groups.google.com/group/mongodb-user/browse_thread/thread/41844e9f87d15816/64a53155c4cc16df

David Mytton at Quora Visit the source

Was this solution helpful to you?

Other answers

You could index a separate key whose value is effectively an array of these other keys. Unpleasant for a variety of reasons (redundant data, you have to keep them in sync, etc.), but it could be made to work without a separate collection.One alternative might be to run a periodic Map Reduce job to automatically construct the "lookup" collection. With MongoDB 1.7.4 your MapReduce job can merge new data.If you are also interested in the values and need to do this type of query for a small set of keys, it may be reasonable to create a sparse index on each key. But that wouldn't be a general purpose solution and is probably not quite what you are looking for. db.coll.ensureIndex({fieldname : 1}, {sparse : true})http://www.mongodb.org/display/DOCS/Indexes#Indexes-SparseIndexes

Robert Stewart

Sounds like what you want is a sparse index. Documentation is here: http://www.mongodb.org/display/DOCS/Indexes#Indexes-SparseIndexes

Jared Rosoff

Non-sparse indexes store null values for documents that do not contain the indexed field. Therefore, you can retrieve such documents using: db.collection.find({"indexed_field":null})

James Daab

some way it can. You can referer to my answer here in stackoverflow. http://stackoverflow.com/a/14627290/544251

Jian Peixin

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.