I'm currently taking a course in university.mongodb but suddenly the queries stopped working. I was practicing with the UpdateOne and noticed that it didn't worked and didn't modified the document I was trying to modify. I'm now trying to do a simple find but it returns a 0 like there's no document in the database, and I already checked if the database was set in the one I had to use.
I reconnected and used the db I needed but the query didn't worked neither.
db.movieDetails.find({title: "The Martian"})
It's not a syntax problem. I expected the output to be 1 because there is only 1 document with that name but it returned a 0.
Screenshot of the console
db.movieDetails.find({title: "The Martian"})
What you are doing is:
db.MovieDetails.find({title: "The Martian"})
Collection name is case sensitive
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
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 am trying to convert a huge set of dates to ISODate objects and storing it in the same collection and field. I am using the following query:
db.collection.find().forEach(function(element){
element.StartTime = ISODate(element.StartTime);
element.StopTime = ISODate(element.StopTime);
db.collection.save(element);
});
The query ran for about 10 minutes, and then gave an error:
2017-06-15T15:48:10.419+0200 E QUERY [thread1] Error: invalid ISO date :
ISODate#src/mongo/shell/types.js:65:1
#(shell):2:23
DBQuery.prototype.forEach#src/mongo/shell/query.js:501:1
#(shell):1:1
I had a look into the entries in the DB and it looks like it did convert a lot of the data set, but I am having troubles now fixing the error and finding the location where it stopped. What I tried is using Studio3T to find where either "StartTime" or "StopTime" has a value of 2017-06-15T15:48:10.419+0200 or at least starts with 2017-06, but as I expected (since there shouldn't be any data from June 2017 in there), it can't find anything.
Now when I run the query again, it gives the error immediately.
My question is if it's possible to find the document in the collection responsible for this error or what the cause might be. Are there recommendations for bulk operations like this preventing these errors?
I clearly don't understand how I update / insert a subdocument in an existing document.
I tried the following:
query = aCollection.find_one({stuffToFind})
aCollection.update(query,
{"$set": {"subDoc" : {"1" : String, "2" : datetime.datetime.now(), "3" : otherString}}})
this only works one time but I want to constantly change the subDoc's Data 1,2 and 3 if this code is executed. find_and_modify also fails, because it seems to overwrite the whole document deleting all other fields but the id and those specified in update.
Since I'm pretty new to MongoDB it would be nice if someone could give me a code example how to figure out my problem.
Edit: without the "$set" statement it remains unchanged as well on a second execution..
Edit2: this seems to work, eventhough I'm unable to edit the affected (JSON)document directly in MonjaDB anymore :D
aCollection.update(query(but this time not as a variable),
{"$set" : {"subDoc.1" : Sting, "subDoc.2" : datetime.datetime.now(), "subDoc.3" : otherString}})
I dont know, why this is working so maybe someone could explain what I did wrong..
Thanks in advance,
Codehai
The query you supply to update in the first example is not correct, instead of:
query = aCollection.find_one({stuffToFind})
you should have:
query = {stuffToFind}
The reason that the update does not throw an error is that the result of find_one is a dictionary. Note also that sometimes the above will even work since in the update you are actually asking MongoDB to match the whole document that corresponds to the initial
query. Subsequent uses of query of course in that case will not bring the expected results since the document will have been changed from the update.
The $set updates only the keys that we specify leaving everything else untouched. That means that if we update an embedded object then the whole embedded object will get replaced with what we specify in $set. If we want to pinpoint keys in the embedded object
we must use dot notation as you do in the second example.
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.