MongoDB : Query with $where and space - mongodb

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"

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.

How to perform a "NOT IN" query in mongodb without using $nin?

I need an alternative to $nin in mongodb. As we know, we can achieve the "IN" query operation by performing "OR" query as well.
In the same way, how can I achieve "NOT IN" functionality in mongodb without using $nin ? What are the alternatives to using $nin in mongodb to acheive exactly same results ?
I found that there is a operator $nor which exactly does this function. It works as the negation of $or.
Well, interesting, But I think It can be achieved by $not
say you have $nin for A,B and C where A,B, C are values which you want to ignore
so instead of calling $nin : [ABC]
what if we call $not : {$or : [A,B,C]}
Update :
Since $not is not working with $or. Maybe you can try $nor which I hope is basically same concept.

Mongodb: return matched filters when using $or in find()

Suppose I am doing a query in Mongodb like this
db.user.find({$or : [{"field1" : "abc"}, {"field2" : "def"}, {"field3" : "ghi"}]})
And a number of documents are returned. What is the easiest way to know which one (or multiple) of the three filters is matched for each document returned? By "easiest", I do not wish to add more executions of find()'s.
Thanks.
There is no such option to solve this on the MongoDB query layer. Likely you want to perform individual queries instead one big $or query in order to solve your problem.

Dot Notation in Node JS MongoDb queries

Is it possible to use Dot Notation when dealing with nested documents?
http://www.mongodb.org/display/DOCS/Dot+Notation+(Reaching+into+Objects)
I'm trying to query the results of a map/reduce and therefore need to
run a query like this:
find({'_id.page' : 'ThisPage', '_id.user' : 'AUser'})
Trying this in Node code returns no rows but the same query works as
expected in mongodb shell.
Dot notation isn't required for reaching inside of documents for queries, you can use document notation instead.
find({'_id.page' : 'ThisPage', '_id.user' : 'AUser'})
could instead be
find({_id: {page: 'ThisPage', user: 'AUser'}})
It is very possible, I've done it before.
Why do you have nested documents under your _id property? Not sure what your use case is but that seems a bit strange. _id is a special property that is always the unique id of the document. So it might be getting treated special by the driver (i.e. doesn't expect there to be sub documents). Maybe try putting your sub-documents under a different property name.