I want use mongoid search like query with integer column.
I know use mongodb can use below command to query
db.test.find({ $where: "/^123.*/.test(this.example)" })
How write it with mongoid?
You know you can use all the usual MongoDB query operators with Mongoid's where so:
Test.where(:$where => '/^123/.test(this.example)')
If you look at the Mongoid::Criteria that that where gives you, you'll see something like this:
=> #<Mongoid::Criteria
selector: {"$where"=>"/^123/.test(this.example)"}
options: {}
class: Test
embedded: false>
and there's the underlying MongoDB query in selector.
BTW, that .* didn't do anything useful in your regex so I took it out.
Related
I am trying to do a simple find in Panache but I'm stuck with the wildcard operator.
I have:
Model.find("payload.tags.name = ?1", "tag-to")
.stream()
.map(m -> (Model) m)
.collect(Collectors.toList());
and my document looks something like this:
{
...
payload:Object{
swagger:"2.0"
info:Object
host:"petstore.swagger.io"
basePath:"/v2"
tags:Array[
0:Object [
name:"tag-to-find"
description:"a tag i want to find"
]
]
}
}
When I try to find "tag-to-find" it works, but I don't know how to get the wildcards going. In mongoshell i just use db.Model.find({"payload.tags.name": /ag-to-/}) and it works.
What you are using in Mongo shell is a JavaScript regex.
You should also be able to use it with MongoDB with Panache.
You should normaly use the regex with the $regex operator, not sure how Mongo shell handle it but the following should work:
Model.list("payload.tags.name like ?1", "/tag-to/")
I use .list() instead of find() as it directly return the list of documents.
The query used here is what we called PanacheQL query that will maps to a MongoDB native query, you can also use a native query directly (with named or indexed parameters).
Simplified query is explained here: https://quarkus.io/guides/mongodb-panache#simplified-queries
I'm having trouble writing the query.
Basically I'm querying on jsonb structure and here is how my sql query looks like:
select (p_product -> 'category_id') from product where p_product-> 'category_id' ?| array['3544', '3179'] limit 10;
Here is the scala code I'm trying to use:
allEvents.filter(row => row.product +> "category_id" ?|.inSetBind(ids.map{_.id}))
This does not work.
On the other hand I'm already able to use the text comparison with something like:
row.product +>> "category_id" inSetBind(ids.map{_.id})
I'm trying to find something online but I'm pretty much out of my own ideas.
I had a look into
https://github.com/tminglei/slick-pg/blob/master/addons/spray-json/src/test/scala/com/github/tminglei/slickpg/PgSprayJsonSupportSuite.scala
So I combined this into:
allEvents.filter(row => row.product.+>("category_id") ?| ids.map{_.id}.toList.bind)
I have 1 issue with mongodb query search with exact values. i want to get collections irrespective of case sensitive. for this i found some querys like below. its working fine.
db.applications.find({"blocks.HOSPITAL_INFO.data.name": new RegExp('^VIKRAM$', 'i')});
in laravel i am using jessengers. . above query i can write as raw query in laravel.
but my issue is when ever i am using $In:{'a','b'} like this how can i write regex for this. FYI 'a','b' are dynamic array values. so how can i write regex for these array values?
The query in MongoDB would be something like this:
db.applications.find({"blocks.HOSPITAL_INFO.data.name":
{$in:[new RegExp('^a$', 'i'),new RegExp('^b$', 'i')]}});
OR, alternatively:
db.applications.find({"blocks.HOSPITAL_INFO.data.name":
{$in:[/^a$/i,/^b$/i]}});
...where a & b are your dynamic variables.
I'm not very familiar with Laravel, but I'm guessing it would look something like this:
$applications = Application::whereIn('blocks.HOSPITAL_INFO.data.name',
[new MongoRegex('^a$/i'), new MongoRegex('^b$/i')])->get();
The most common thing I do in the mongo DB shell is find objects by ID, eg:
db.collection.find({_id: ObjectId("55a3e051dc75954f0f37c2f2"})
I do this over and over and I find having to wrap the id with ObjectId over and over gets old. I wish I had a findById-like shorthand form like what mongoose provides. I feel like the shell ought to be smart enough to figure out what I mean here for example:
db.collection.find("55a3e051dc75954f0f37c2f2")
How might I do this? Or are there any alternative ways of querying by id in the mongo shell?
Fortunately, you can extend the shell quite easily, for example by adding the following method to the ~/.mongorc.js file which is executed when you start the mongo client:
DBCollection.prototype.findById = function(id) {
return db.getCollection(this._shortName).find( { "_id" : ObjectId(id) } );
}
Then you can execute something like db.collection.findById("55a3e051dc75954f0f37c2f2")
The shorthand for find({_id: ObjectId("...")}) is find(ObjectId("...")).
I am trying to use map_reduce on a collection, via the ruby console , but I am getting "NoMethodError: undefined method `map_reduce' for #
results = Thing.collection.map_reduce(map, reduce, out: "vr")
Map Reduce in Mongoid 3 works slightly differently. The syntax you have would work for the mongo ruby driver. In Mongoid 3, you call this off the class or criteria, like the following:
From a criteria:
Model.where(field: value).map_reduce(map, reduce).out(inline: true)
From a class:
SomeClass.map_reduce(map, reduce).out(replace: "mr-results").each do |document|
#do something
end
You can find more information on this in the Mongoid docs