How can I perform like query for big decimal number containing decimal part in it in mongodb - mongodb

I have perform like query in mongodb. I have following document in employee collection as:
{
"_id" :1
"name":"Abc",
"salary": 56789.98456
}
So far,I have performed like query to match the above employee document. I have tried following queries in mongo:
db.employee.find({"salary":/.*56789.98.*/}).pretty()
db.employee.find({"salary":/^56789.98/}).pretty()
db.employee.find({"salary":/56789.98/}).pretty()
But it doesn't work.
I also looked at:
How to query MongoDB with "like"?
How do I achieve this in mongo ?

You can do something like this:
db.employee.find({"$where":"/^56789.98.*/.test(this.salary)"})

Related

How to use query commands in MongoDB?

MongoDb query
I am new to MongoDB, I just started learning recently.When I am using a query command for instance, db.tests.find({"by":"Srihari"}) .It is not giving any output. Is there any wrong with my query? Please help!
From the screenshot you've shared following document exists in your tests collection:
{"username": "srihari"}
{"username": "srih"}
{"username": "srh"}
{"username": "sh"}
The query you're sending to mongodb is :
db.tests.find({"by":"Srihari"})
There isn't any document in tests collection that matches your query.
However, you can query like this:
db.tests.find({"username": "sh"})
will definately return the result.
In MongoDB you specify equality conditions, using <field>:<value> expressions in the query filter. So db.tests.find({"by":"Srihari"}) is looking for all documents where the field "by" has the value "Srihari".
Since your document has the format
{
username: "srihari"
}
your query should be:
db.tests.find({username: "srihari"})
You can see more examples here: https://docs.mongodb.com/manual/tutorial/query-documents/

Order of Fields in Mongo Query vs Ordered Checked In

Say you're querying documents based on 2 data points. One is a simple bool parameter, and the other is a complicated $geoWithin calculation.
db.collection.find( {"geoField": { "$geoWithin" : ...}, "boolField" : true} )
Will mongo reorder these parameters, so that it checks the boolField 1st, before running the complicated check?
MongoDB uses indexes like any other DBs. So the important thing for mongoDB is if any query fields has an index or not, not the order of query fields. At least there is no information in their documentation that mongoDB try to checks primitive query fields first. So for your example if boolField has an index mongoDB first check this field and eliminate documents whose boolField is false. But If geoField has an index then mongoDB first execute query on this field.
So what happens if none of them have index or both of them have? It should be the given order of fields in query because there is no suggestion or info beside of indexes in query optimization page of mongoDB. Additionally you can always test your queries performances with just adding .explain("executionStats").
So check the performance of db.collection.find( {"geoField": { "$geoWithin" : ...}, "boolField" : true} ) and db.collection.find( { "boolField" : true, "geoField": { "$geoWithin" : ...} } ). And let us know :)
To add to above response, if you want mongo to use specific index you can use cursor.hint . This https://docs.mongodb.com/manual/core/query-plans/ explains how default index selection is done.

mongodb $addToSet failure, specify full document to insert

I've done a bit of research on this and haven't come across anything that jumps out at me immediately as what I'm looking for.
Say we have a document (or documents) in a collection that look something like this:
//First example document
{
"_id": "JK",
"letters": ["J", "K"]
}
//Second example document
{
"_id": "LM",
"letters": ["L"]
}
So I run a query like the one below to see if I have any matching documents and of course I don't so I expect to get null.
> db.example.findOne({"_id": "LM", "letters": {"$in": ["M"]}})
null
So I do an update and add "M" to the letters array on the documents (syntax may not be quite right):
> db.example.update({"_id": "LM"}, {"$addToSet": {"letters": "M"}})
I run the possibility of not having a matching _id, so the findOne would would also return null given the example documents in the collection for this query.
> db.example.findOne({"_id": "AB", "letters": {"$in": ["A"]}})
null
Based on the way I've constructed the above query, I get null back when "A" is not found in letters or the _id of "AB" is not found on any document. In this case I know that this document isn't in there because I know what is in the collection.
What I'd like to do is keep my update query from above with $addToSet and modify it to use upsert WHILE ALSO specifying the document to insert in the event that $addToSet fails due to the document not existing to cut down on database transactions. Is this possible? Or will I have to break up my queries a bit in order to accommodate this?
Because this information may influence answers:
I do my querying through mongo shell and pymongo.
mongo version: 2.6.11
pymongo version: 2.8
Thanks for any help!
EDIT: So after a break and a bit more digging, it seems setOnInsert does what I was looking for. I do believe that this probably solves my issue, but I've not had a chance to test yet.

to compare two fields of the same collection

I want to compare two fields of the same collection (Mysql query example "SELECT * FROM table AS t WHERE t.field1 > t.filed2;") in mongodb with cakephp. I cannot use '$where' and aggregate of mongodb as I am also using other operators of mongodb like $or, $and and etc. And also I am using find of mongodb.
Ex: Collection have two fields integer fields per_day_budget and today_spent and I want to get the list of records where today_spent is less than or equal to per_day_budget. I hope this will you to better understand my query.
Kindly suggest solution for the same.
You can try:
db.collection.find({ this.today_spent : {$lte : this.per_day_budget}});

Mongo remove last documents

I would like to know how to delete, for example, the last 100 documents inserted in my collection.
How is it possible from the shell?
You should be able to use the _id to sort on last inserted, as outlined in the answer here:
db.coll.find().sort({_id:-1}).limit(100);
It looks like using limit on the standard mongo remove operation isn't supported though, so you might use something like this to delete the 100 documents:
for(i=0;i<100;i++) {
db.coll.findAndModify({query :{}, sort: {"_id" : -1}, remove:true})
}
See the docs for more on findAndModify.