mongodb wildcard query from grails/groovy - mongodb

I’m having some problems with issuing a wildcard query in MongoDB from my Grails application.
Basically the way I am doing it now is by issuing a find query with an array of query parameters:
db.log.find(criteria) -> where criteria is an array [testId:"test"]
This works fine as long as I’m strictly querying on actual values. However, for fun, I tried it with a wildcard search instead:
db.log.find(criteria) -> this time critera = [testId:/.*te.*/]
This however will after looking at the Mongo query log as:
query: { query: { testId: "/.*te.*/" }
hence making the query not a wildcard search, but a query for this as a string, instead.
Is there a way to work around this in some sense still using this concept of querying?
Thanks in advance!

Use the Groovy Pattern shortcut ~ to specify that your query is a regular expression.
db.log.find(['testId': ~/.*te.*/])
See this blog post for more info

To use regex query, define query condition with $regex operator
def regexCondition = ['$regex': '/.*te.*/']
def criteria = ['testId': regexCondition]
db.log.find(criteria)

This worked for me:
In your groovy file:
db.collectionName.find([fieldName:[$regex:'pattern']])
More or less, use a regular mongodb query, but replace the {} with [].

Related

How to find all documents with field values that start with a given string in MongoDB?

I'm trying to do a query in mongodo where I have get all documents that start have a type that starts with a value - eg ea
I have the following in my pipeline
$matchPipeline["TYPE"] = ['$in'=> ["EAVWF", "EA"]];
I would like to find all types that start with 'EA', so I don't have to type them out.
You may use regex
db.getCollection('collectionname').find({"TYPE": {"$regex": "^EA"} })
Startwith queries can potentially use indexes. So, It will perform good if you create index on TYPE.
By default $regex is case sensitive
Case Insensitive version. Would perform poorly on large data
db.getCollection('collectionname').find({"TYPE": {"$regex": "^EA","$options": 'i'} })

mongoDB spring-boot custom query in uppercase, with LIKE

I need to search the items by their names via spring boot on a db implemented by mongodb. For a normal SQL database I would've done as follows:
#Query("SELECT * FROM customer WHERE UPPER(name) LIKE UPPER(CONCAT('%', :name, '%'))")
List<Customer> findByName(#Param("name") String name);
Yet it does not accept a normal 'SELECT' query, because it is a NoSQL database. Thus I need to query this collection by using JSON format, yet I have no idea of the implementation on spring-boot.. This is my first time using mongodb too.
Going through the posts on stackoverflow, I have found an example such as:
#Query("{'name':?0}")
And on this site there are examples. Also here there is an explanation of the query above. Yet still I have no idea how to convert the former query I have pasted above into a "json based query".
Note: I am extending MongoRepository on my repository.
Note2: The above query is case sensitive, that's the reason I am trying to query in upper case along with converting the column data into upper case.
UPDATE
#Query(" { $text: { $search: ?0 , $caseSensitive: false } }")
works perfectly(I had to index my collection by launching this command:db.customerCollection.createIndex({name: "text"}) ) to ignore case sensitivity, but I still need to implement 'LIKE'; because when I search 'user', it is able to find 'UsEr' etc. But if I search 'se', it doesn't bring me any result. Any help will be appreciated!
Thank you for your time!
I have resolved my issue by using $regex and writing the query as:
#Query(" { companyName: { $regex : '(?i)?0'} } }")
I would've never guessed to spend that much time for such a simple thing. But I am glad that it is resolved anyway !
[ref]

Pymongo: iterate over all documents in the collection

I am using PyMongo and trying to iterate over (10 millions) documents in my MongoDB collection and just extract a couple of keys: "name" and "address", then output them to .csv file.
I cannot figure out the right syntax to do it with find().forEach()
I was trying workarounds like
cursor = db.myCollection.find({"name": {$regex: REGEX}})
where REGEX would match everything - and it resulted in "Killed".
I also tried
cursor = db.myCollection.find({"name": {"$exist": True}})
but that did not work either.
Any suggestions?
I cannot figure out the right syntax to do it with find().forEach()
cursor.forEach() is not available for Python, it's a JavaScript function. You would have to get a cursor and iterate over it. See PyMongo Tutorial: querying for more than one document, where you can do :
for document in myCollection.find():
print(document) # iterate the cursor
where REGEX would match everything - and it resulted in "Killed".
Unfortunately there's lack of information here to debug on why and what 'Killed' is. Although if you would like to match everything, you can just state:
cursor = db.myCollection.find({"name": {$regex: /.*/}})
Given that field name contains string values. Although using $exists to check whether field name exists would be preferable than using regex.
While the use of $exists operator in your example above is incorrect. You're missing an s in $exists. Again, unfortunately we don't know much information on what 'didn't work' meant to help debug further.
If you're writing this script for Python exercise, I would recommend to review:
PyMongo Tutorial
MongoDB Tutorial: query documents
You could also enrol in a free online course at MongoDB University for M220P: MongoDB for Python Developers.
However, if you are just trying to accomplish your task of exporting CSV from a collection. As an alternative you could just use MongoDB's mongoexport. Which has the support for :
Exporting specific fields via --fields "name,address"
Exporting in CSV via --type "csv"
Exporting specific values with query via --query "..."
See mongoexport usage for more information.
I had no luck with .find().forEach() either, but this should find what you are searching for and then print it.
First find all documents that match what you are searching for
cursors = db.myCollection.find({"name": {$regex: REGEX}})
then iterate it over the matches
for cursor in cursors
print(cursor.get("name"))
The find() methods returns a PyMongo cursor, which is a reference to the result set of a query.
You have to de-reference, somehow, the reference(address).
After that, you will get a better understanding how to manipulate/manage the cursor.
Try the following for a start:
result = db.*collection_name*.find()
print(list(result))
I think I get the question but there's no accurate answer yet I believe. I had the same challenge and that's how I came about this, although, I don't know how to output to a .csv file. For my situation I needed the result in JSON. Here's my solution to your question using mongodb Projections;
your_collection = db.myCollection
cursor = list(your_collection.find( { }, {"name": 1, "address": 1}))
This second line returns the result as a list using the python list() function.
And then you can use jsonify(cursor) or just print(cursor) as a list.
I believe with the list it should be easier to figure how to output to a .csv.

I am a new user of Mongo Db, please let me know the difference in executing both queries

Hi I found that the output of the two queries was same, but I want to know is there any difference in executing the query.
First:
db.collectionname.find({}).pretty()
Second:
db.offers.find().pretty()
There is no difference between these two queries.
db.collectionname.find({}).pretty()
You are not giving any query params here so the results are same like
db.collectionname.find().pretty()
In short: Both function in the same way.
1) db.collectionname.find({})
Here you are not specifying any query parameters and its just an empty document {} so it will return you all the documents present in the collection
2) db.offers.find()
Here you didn't specify any queries. So it need not even look at the parameters, it will just print you all the documents in the collection. find() short form of find({})

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