How to use query commands in MongoDB? - 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/

Related

MongoDb db.collection.find() With Special Character

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 :

MongoDB $regex with $in clause

I need a mongodb query something like
db.getCollection("xyz").find({"_id" : {$regex : {$in : [xxxx/*]}}})
My Use case is -- I have a list of Strings such as
[xyz/12/poi, abc/98/mnb, ytn/65/tdx, ...]
The ids that are there in the collection(test) are something like
xyz/12/poi/2019061304.
I will get the values like xyz/12/poi from the input list, the other part of the id being yyyymmddhh format.
So, I need to go to the collection and find all the documents matching the input list with the ID of the documents in the test collection.
I can retrieve the documents individually but that does not seem to be a feasible option as the size of the input list is more than 10000.
Can you guys suggest a more feasible solution. Thanks in advance.
I tried using $in with $regex. But it seems mongodb does not support that. I have also tried pattern matching but even that is not feasible for me. Can you please suggest an alternative to using $in with $regex in mongodb.
Expected result could be an aggragate query/a normal query so that we hit the database only once and get the desired output rather than hitting the db for 10000 odd times.

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());

mongodb empty is the wildcard but only on top level?

sorry to those who do not like Python my samples use pymongo.
In mongodb empty is used as "wildcard" like the here (using the "wildcard" I get same or more results):
>>> projects.find_one({'name': 'boodo3', '_id': ObjectId('4efa14c6b0ab584326000000')})
{u'_id': ObjectId('4efa14c6b0ab584326000000'), u'name': u'boodo3', u'updated_at': u'2011-12-28 13:47:09'}
>>> projects.find_one({'name': 'boodo3'})
{u'_id': ObjectId('4efa14c6b0ab584326000000'), u'name': u'boodo3', u'updated_at': u'2011-12-28 13:47:09'}
but if I am querying "inside" of the document the wildcards do not work any more like in the following sample:
>>> testruns.find_one({'_parent': {'_coll': u'projects', '_id': '4efa14c6b0ab584326000000'}})
{u'_parent': {u'_coll': u'projects', u'_id': u'4efa14c6b0ab584326000000'}, u'_id': ObjectId('4efa167eb0ab584351000002'), u'name': u'11121617', u'updated_at': u'2011-12-27 19:03:26'}
>>> testruns.find_one({'_parent': {'_coll': u'projects'}})
>>> <no results here>
I tried variations using '$in' and '$nin' but so far without luck. I hope there is some structured way of querying documents (I mean something besides regex). I would restructure my collections if necessary but I believe that flat documents are not the solution here.
Does that mean I have to translate all the queries into dot-notation?
Or are regex the way to query documents in mongodb?
disclaimer: I do not intent to criticize mongodb or anything. This is my first app which uses mongodb and I want to learn how to query documents in mongodb.
This query:
testruns.find_one({'_parent': {'_coll': u'projects'}})
Is looking for documents where the _parent is equal to {'_coll': u'projects'} which does not have any matches because the document has other keys in it as well, so equality is not satisfied - there is no notion of "wildcard" here. Try your query like this:
testruns.find_one({'_parent._coll': u'projects'})
Please update the title as the question itself is incorrect.
"How to search nested document criteria in mongodb" which is a duplicate:
How to correctly query a MongoDB nested document with python?
This is also answered in the mongodb docs here:
https://docs.mongodb.com/manual/tutorial/query-embedded-documents/
Under "Specify Equality Match on a Nested Field"

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