Collection.find is not working properly - mongodb

I am using TingoDB and it uses all Mongodb syntax.
I have one collection where I am saving around 70K documents but when I run find query without any filter it returns only around 42K documents.
I do not know what I am doing wrong.
Please find below code,
collection().find().count(function(err, c){
console.log("count is: ",c);
console.log("err is: ",err);
});
I also checked the file where records are saved and I am sure not all records are returning.
Can somebody please help me out here.
TIA
Mangesh

You can try following things
1. Check how many records are saved in DB. Is it 70k unique records in collection.
2. Run same query using DB Client. You can run similar query using any DB Client and check if same query returns similar records.

Related

Mongo query is running endlessly

Something went wrong and I don't understand why. I have a script that used to be working but all of the sudden if just stopped. It's basically a mongo query on a big Mongo collection (600gb+).
Here's the query:
db.action_traces.findOne( {"block_time": {"$lt": "2018-07-15T00:00:00.000Z"} } ).pretty()
Originally I wasn't using findOne but I've restricted the results to one just in case I could help but the result is the same: nothing happens.
If I just run a find query it goes fine.
There is nothing showing up in the mongodb log and nothing seems relevant in the syslog either.
There is clearly something wrong with Mongo though, as htop is showing me this:
The mongo process is fluctuating but is most of the time filling out one CPU thread to 100%.
Can anyone help?
Many thanks in advance,
Sounds like its probably trawling through the whole collection - have you added an index on block_time ?
.find() returns a cursor pointing to the first set of matching documents, ordered by default (natural order) if you dont specify a sort order.
.findOne() returns a single document - with the provisio that if your query matches multiple documents then it uses their "natural order" and returns the first one (implying that it needs to find all matches first).
I'm guessing that your collection needs an index on that field - meaning it can look up the result without a full scan.

Updating data in Mongo sorted by a particular field

I posted this question on Software Engineering portal without conducting any tests. It was also brought to my notice that this needs to be posted on SO, not there. Thanks for the help in advance!
I need Mongo to return the documents sorted by a field value. The easiest way to achieve this would be running the command db.collectionName.find().sort({field:priority}), however, I tried this method on a dummy collection of 1000 documents; it runs in 22ms. I also tried running db.collectionName.find() on the same data, it runs in 3ms, which means that Mongo is taking time to sort and return the documents (which is understandable). Both tests were done in the same environment and were done by adding .explain("executionStats") to the query.
I will be working with a large amount of data and concurrent requests to access DB, so I need the querying to be faster. My question is, is there a way to always keep the data sorted by a field in the DB so that I don't have to sort it over and over for all requests? For instance, some sort of update command that could sort the entire DB once a week or so?
A non-unique index with that field in this collection will give the results you're after and avoid the inefficient in-memory sorting.

Mongo DB remove recently inserted items

I have the following problem. I inserted data using a random data generator into a mongodb database. Problem was I chose the wrong DB connection and now I have test data in my db.
Is there any way to roll back the insert operations or delete all item that where recently inserted? Does mongodb track the time by default?
Thank you for your answers :)
So thanks to the answer of Neil Lunn, I got on the right track. While it is indeed not possible to rollback a write, I was able to detect the Creation Time and delete the data that was created in that time span.
I used this answer
Can I query MongoDB ObjectId by date?
in combination with find and remove, to remove the entities from my collections in my db.
I was able to simply copy and paste the function into the mongodb console and from there used the function on my collections.
Thank you again for pointing me in the correct direction.

How do I get feedback on whether a MongoDB query succeeded and how many rows were changed?

I have a MongoDB database and am using MongoChef to write scripts for it. I have a script that reads in data from a collection and inserts the records into another collection. The script runs fine, but I don't get any feedback on what occurred. Is there a way to get acknowledgement that the script has finished running (that is, all the records are inserted)? Is there a way to get an output of how many records were (in this case) inserted? (I realize I could write another statement to count records, but I want to know how many records were actually inserted by the insert statement). What I'd like to see is something like "Script successful. 1200 records inserted into collection properties." Can someone show me how to turn on this output for MongoChef? Thank you.
Below is an image of my script. This is after it's been run. Notice that there's nothing in the results tabs; there's no indication the queries have been run, that they were run succesfully or how many records have been updated.
You can go through the MongoDb documentation for WriteConcerns and see what information matches your Mongodb version. Previously the getLastError was used to get error information about the last executed CRUD statement.
getLastError can give you information if there's any error occurred post execution of any CRUD operation.
You can also use the WriteResult which is return by insert, update, remove and save operation to get the number of updated document. It also contains properties like writeError to get the information specific to that opeartion.
Sample(psuedo not specific to MongoChef) -
var wr = db.properties.insert(doc);
println("Updated %d collections of type %s", wr.getN(), type);

How can I get the collection name a Mongo document belongs to?

How can I determine what collection a Mongo document belongs to?
I'm using MeteorJS, but even if you don't use this, I'm pretty sure I have access to MongoDB's internal commands so please answer anyways.
Happy to close if this is a duplicate, but I didn't find anything close to this.
There isn't a real short command but to cycle through each collection and query for the field, or fields that uniquely identifies this document. Don't know MeteorJS but if you are looking for just a quick way to get the data to find out and not add a program function you can download RoboMongo. Think of it as SQL Management Studio for MongoDB. If you are looking for a program function to do this then I suggest you make it a jscript function you create inside MongoDB (similar to stored procedures) to do the work and return the results to you when done.
Are you querying the document now and wondering where it is coming from? If so, it should be in the code.