I'd like to query a mongo collection for records which either don't have a value for a field named 'scheme', or explicitly have the value 'http' for 'scheme'. Sounds pretty easy, but this problem has proved more complex than it first appears.
Since db.collection.find({'scheme': None}) returns all records where 'scheme' is undefined (no index field), I initially assumed the following would work:
db.collection.find({'scheme': {'$in': ['http', None]}})
However, this seems to exclude values in which 'scheme' is undefined, so I can only assume it is searching for records where scheme is either 'http', or explicitly defined to be None. This seems to be a bit counterintuitive, but there we have it. My second attempt was the following:
db.collection.find( {'$or': [{'scheme': {'$exists': False}}, {'scheme': 'http'}]})
This also excludes result where scheme is undefined. This time, I can't even think of a logical reason why this is failing.
Any ideas why this is failing, and how I can get it to work as desired?
Thanks
EDIT: Just thought I'd note that I'm performing this query through Python (pymongo), which explains the None (over Javascript's null)
Resolved: This is apparently an issue of my version of mongodb (1.4.4), the issue is solved in 1.6.5.
Related
community!
I am in a weird situation. The direct equally check returns result, but when using $in I am not getting any records.
db.getCollection("voter").find({"id":{$in:["db1eefc5-09ad-4d4f-a31a-db63d8261913"]}})
db.voter.find({"id":{$in:["db1eefc5-09ad-4d4f-a31a-db63d8261913"]}})
Doesn't return anything.
db.voter.find({id: "db1eefc5-09ad-4d4f-a31a-db63d8261913"})
Returns the desired record.
Being more of a fullstack developer, I don't know what's happening in-depth, but I am sure that both things shall work ideally which is not the case here.
Extra info:
I have defined hashed unique indexes on id.
Thanks.
The problem is pretty simple:
On the first screen you're running your query against admin database
while second query gets executed against crmadmin db
As the Title suggests, I found a rather strange behavior which I am not sure to attribute to Studio3T or to MongoDB.
I have a collection containing simple documents, as follows:
Example of a document in the collection
Whenever I perform a query using the query builder, everything works as expected. However, when I do it using IntelliShell or Aggregate AND if such query is based on the field StudyID, no document is returned, regardless the query.
For example, the two following queries return, respectively, an empty list and no documents.
db.Cortisol.distinct("StudyID")
db.Cortisol.find({},{"StudyID" : 1})
While the following very similar query returns the documents, as expected.
db.Cortisol.find({},{"ExamID" : 1})
As you can see the queries are correct, I did not misspell anything so does anyone have a possible explanation for such a strange behavior?
I wonder if your field "StudyID" contains some weird (invisible) unicode chars. This would explain why dragging the field into query builder works (because the query builder uses the exact same string) and typing S-t-u-d-y-I-D doesn't.
When you run an empty query and you see documents with StudyID returned, can you then copy the field name of your StudyID field to the clipboard, and then when you type your query in IntelliShell paste the StudyID name?
I am using Pymongo (v3.5.1) in a Python v3.6.3 Jupyter notebook.
Problem
Even-though I am limiting my results, the db.collection.find() is still retrieving all results before returning
My code:
for post in posts.find({'subreddit_1':"the_donald"}, limit=2):
print(post)
exit
Background
I have imported the Reddit comment data set (RC_2017-01) from files.pushshift.io and created an index on the subreddit field (subreddit_1).
My Indexes
I believe this is caused by the collection having no index on your query term, as exhibited by the line:
planSummary: COLLSCAN
which means that to answer your query, MongoDB is forced to look at each document in the collection one by one.
Creating an index to support your query should help. You can create an index in the mongo shell by executing:
db.posts.createIndex({'subreddit_1': 1})
This is assuming your collection is named posts.
Please note that creating that index would only help with the query you posted. It's likely that different index would be needed for different type of queries.
To read more about how indexing works in MongoDB, check out https://docs.mongodb.com/manual/indexes/
I think you need to change the query, because in find() method 2nd parameter is projection. Find() always return cursor and limit function always works on cursor.
So the syntax should like below:
for post in posts.find({'subreddit_1':"the_donald"})[<start_index>:<end_index>]
print(post)
exit
OR
for post in posts.find({'subreddit_1':"the_donald"}).limit(2)
print(post)
exit
Please read the doc for detail
I have a dataset whose schema is like this:
{..., "url":"www.google.com", "time::143703672, "geo":"US-NJ", ...}
I want search documents by: (I'm using pymongo)
data.find({'url':url, 'geo':user_geo, 'time':{"$gt": userlog_gmt_time - 10, "$lt": userlog_gmt_time + 10}})
I tried to build a compound index:
db.AdxRevenueData_Jul.createIndex({url:1, geo:1, time:1})
However, I receive an error "key too long to index". The reason is that some url is extremely long. I believe that there are some "wrong" url in the dataset.
Then, I tried to build a compound index based on only 'geo' and 'time'. I thought I could iterate the return result and find the documents with the url. However, this method was too slow...
Someone suggested me to set parameters to skip those long urls.
sudo mongod --setParameter failIndexKeyTooLong=false
But, I am not sure if all long urls are wrong. I do not want to skip a lot documents.
My question is, is there any other solution, except changing DB?
For instance, should I use "text" index? Will it work?
db.AdxRevenueData_Jul.createIndex({url:'text', geo:1, time:1})
Or,
Should I additionally build url as a 'hashed' single index? Can it be a compromise?
db.AdxRevenueData_Jul.createIndex({url:'hashed'})
db.AdxRevenueData_Jul.createIndex({geo:1, time:1})
Using text index will not be helpful there.
I would recommend to use hashed index for this field.
so i created a collection called food with 2 objects that were saved no problem. Using find() yielded no issues either. However, when I entered the following:
db.food.find().sort({averageRating:-1}).select({_id: 1}).limit(2)
I get the error:
JS Error: TypeError: Object DBCursor has no method 'sort'
What am i doing wrong?
Is this what you are looking for?
db.food.find({},{_id:1}).sort({"averageRating":-1}).limit(2);
It selects only 2 id fields ordered by average rating descending.The fields that are to be returned are specified by the second parameter in find(),which in this case is _id.
select is not a valid command in mongoDb as far as I know.
It should be selector, not select. See if that fixes it.
As per shargors' comment, it looks like try.mongodb.org doesn't support sort(). I would recommend downloading and installing mongodb itself, and playing around with the real shell.