MongoDb db.collection.find() With Special Character - mongodb

I am trying to write a simple query in MongoDb using PyMongo driver as such
mongo.db.people.find_one({'name': 'Tést Name'})
mongo.db.people.find_one({'name': 'Test_O%27Name'})
Both of these queries are returning null. I have checked to make sure the data exists in the db. How do I change the query so that find() is able to find it?
Thanks

You can use like this
db.myCollection.find({ myKey : /.*\$tes.*/i });
You have to escape $ by :

Related

MongoDB Query - Filter only works with "_id"

I'm using mongodb v4.0.3, and this happens both with the shell and in compass.
This only happens with a certain collection. It previously had overriden ids (instead of the default mongodb id, there was a string. I dropped the collection and I recreated it without that).
I have the following structure ("mystructure"), for example:
{
"_id":ObjectId("5bd44eb528d61e3374b5e6ea"),
"custom_field":"data",
}
When I query it without a filter it returns all the docs:
db.mystructure.find({});
When I search for its objectid, it returns properly
db.mystructure.find( {"_id": ObjectId("5bd44eb528d61e3374b5e6ea")} );
But when I try to filter with any field, it doesn't return anything
db.mystructure.find( {"custom_field": "data"} );
At first I thought it would be solved recreating the collection with the automatically generated ids from mongodb but the problem persists. There are no "hidden" spaces in the string or anything like that. The same query in compass isn't working either. Other collections do work. It's on the same db, with the same user.
Why can this be?
Thank you very much.
you should write below code where 62c01e5a763d106152a2e53f is your _id
{_id:ObjectId('62c01e5a763d106152a2e53f')}

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/

MongoDB : Query with $where and space

I need to use $where in a mongoDB query like following :
$where:"this.extracted_service.DATA.MYKEY WITHSPACE.length >1"})
The problem is one of the key has a space in it so It won't work like that.
Any idea how to do it ?
Replace that with
$where:"this.extracted_service.DATA['MYKEY WITHSPACE'].length >1"

MongoDB $or query in Meteor?

The mongodb $or operator works as intended outside of a meteorjs context:
db.users.find({$or: [{email: 'some#mail.com'},{city: 'atlanta'}]});
I get results for any document that has email some#mail.com or city of atlanta.
The same query in Meteor syntax doesn't yield the same results :
Users = new Meteor.Collection("users");
Users.find({$or: [{email: 'some#mail.com'},{city: 'atlanta'}]});
I've read the meteor docs - http://docs.meteor.com/#find - and since it doesn't say anything about it, I'm assuming it should run just the same as a mongodb 1.6+ instance?
find returns a cursor object. You need to use a fetch to get the array of values. Try:
console.log(Users.find({$or: [{email: 'some#mail.com'},{city: 'atlanta'}]}).fetch());

how do I exlude term in mongodb query

I am having urls in this format stored in mongodb
Source:
index.php?name=xxxxxxxxxxxxxabcxxxxxxxx&id=15&success=1
index.php?name=xxxxxxxdefxxxxxxxxxxxx&id=18&success=0
where xxxxxxxxxxxxxxx is some string
I want to write a query to find all sources where name should not contain "abc" as a substring
So I wrote the query
db.coll.find({source:/(?!name=abc)/})
but this query is not working..please guide me what will be the correct query
db.coll.find({source: {$not: /[?&]name=.*abc.*(&|$)/}})
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-Metaoperator%3A%24not
regex w/ $nin (not in). Don't think that is supported as a single query yet...
try looking at
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24nin
http://jira.mongodb.org/browse/SERVER-322
https://github.com/mongodb/mongo/commit/6c7dc2b0f8831fac6621f125889d873241588b02