I am storing regex expressions in MongoDB and would like to use them for queries.
Documents structure:
{
"name": "foo",
"regex_expression": "^[^# ]+#(bar\\.com)$"
}
I tried the following query but it doesn't work.
db.collection.find({$expr: {$regex: ["foo#bar.com", "$regex_expression]}})
Is it possible to do this kind of query?
You need the $regexMatch operator.
db.collection.find({
$expr: {
$regexMatch: {
input: "foo#bar.com",
regex: "$regex_expression"
}
}
})
Demo # Mongo Playground
Related
I am trying to search for a keyword inside array of arrays in a Mongo document.
{
"PRODUCT_NAME" : "Truffle Cake",
"TAGS": [
["Cakes", 100],
["Flowers", 100],
]
}
Usually, I would do something like this and it would work.
db.collection.find( {"TAGS":{"$elemMatch":{ "$elemMatch": {"$in":['search_text']} } }} )
But now, I changed this query to an aggregate based query due to other requirements. I've tried $filter , $match but not able to replicate the above query exactly..
Can anyone convert the above code so that it can directly work with aggregate?
(I use PyMongo)
$match uses the same query syntax as the query language (find), from the docs:
The query syntax is identical to the read operation query syntax;
This means if you have a query that works in a "find", it will also work within a $match stage, like so:
db.collection.aggregate([
{
$match: {
"TAGS": {
"$elemMatch": {
"$elemMatch": {
"$in": [
"Cakes"
]
}
}
}
}
}
])
Check this live on Mongo Playground
Want to search all matching pattern from Mongo DB nested fields with dynamic keys.
DB Structure:
_id: 'dsdsdsadadad',
results: {
tables: {
jvm: {
data: [
{
Prediction: 1,
Jvm: 'service_name',
Status: 'OK'
},
{
second: 'New second set'
}
}
}
}
Tried By $,
db.col_name.find('results.tables.jvm.data.$.Jvm': {'$regexp': 'service.*'})
By using $i
db.col_name.find('results.tables.jvm.data.$i.Jvm': {'$regexp': 'service.*'})
By giving particular key 0 also,
db.col_name.find('results.tables.jvm.data.0.Jvm': {'$regexp': 'service.*'})
No results!
Expected O/P:
The above doc and where all Jvm starts with service* keyword
Thanks,
You should directly use the dot notation to query an array of nested objects:
db.collection.find({ "results.tables.jvm.data.Jvm": { $regex: "service.*" } })
MongoDB will try to find every document that contains at least one nested document under data having Jvm field matching your regex.
MongoDB Playground
I have a cloudant nosql db with some records like this:
{
"role": "utente",
"nome": "Rocco",
"cognome": "Di Vitto",
"team": "sap pm-cs",
"company": "wrestling",
"appManager": "john Ford",
"teamLeader": "Rendy Orton, Colombo M.R."
}
I would like to query my db with a $regex the teamLeader field, passing a single string that matches either with "Randy Orton" or "Colombo M.R." but I can't figure out the regex for matching two comma-delimited strings. I've tried reading Erlang queries structure but I didn't find the solution. Some help would be very useful. thanks in advance
when supplying regular expressions you should be using the $regex operator. have you tried something like this:
{
"selector": {
"teamLeader": {
"$regex": "(Colombo M.R.)|(Rendy Orton)"
}
}
}
I want to perform searching using regular expression involving whitespace in elasticsearch.
I have already set my field to not_analyzed. And it's mapping is just like
"type1": {
"properties": {
"field1": {
"type": "string",
"index": "not_analyzed",
"store": true
}
}
}
And I input two value for test,
"field1":"XXX YYY ZZZ"
"field1":"XXX ZZZ YYY"
And i do some case using regex query /XXX YYY/ (I want to use this query to find record1 but not record2)
{
"query": {
"query_string": {
"query": "/XXX YYY/"
}
}
}
But it return 0 results.
However if I search without using regex (without the forward slash '/'), both record1 and record2 are returned.
Is that in elasticsearch, i cannot search using regex query involving space?
What you need is a ''term'' query that doesn't tokenise the search query by breaking it down into smaller parts. More about the term query here: https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-term-query.html
There's a special breed of term queries that allows you to use regexes called regexp queries. That should match any whitespaces as well: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html
You can keep using your query string, but your regexp is just missing a tiny part, i.e. the .* at the end. If you run that you'll get the single result you expect.
{
"query": {
"query_string": {
"query": "/XXX YYY.*/"
}
}
}
You can use regexp queries to achieve this. Mind you, the query performance may be slow. The below query will search for all documents in which the value of field1 contains "XXX YYY".
POST <index_name>/type1/_search
{
"query": {
"regexp": {
"field1": ".*XXX YYY.*"
}
}
}
I have a mongo collection User which contains data like:-
{
id : 1,
name : "gaurav",
skills : "C++ HTML CSS"
}
when I am searching for all users that have C++ skill in it with the following query I am getting correct results as expected
db.user.find({skills:{contains:"C++"}});
But when I am searching all the unique names from the user using the same condition I m not getting any desired result
db.user.distinct('name',{skills:{contains:"C++"}});
Can anyone help me with what I am doing wrong?
The "contains" is not a valid keyword for MongoDB queries. You need $regex which submits a general "regular expression" statement matching the pcre specifications:
db.user.distinct( "name", { "skills": { "$regex": "C\+\+" } })
If using JavaScript as you language then this is also safe:
db.user.distinct( "name", { "skills": /C\+\+/ })
To determine if the string "C++" occurred somewhere within the string value of the field being tested. The + character is reserved in "regex" operations and therefore you need to escape it with a \ char as the standard escaping mechanism.
On your data this is the result:
db.user.distinct( "name", { "skills": { "$regex": "C\+\+" } })
[ "gaurav" ]
Try to use REGEX like below query
db.user.distinct("name",{"skills":{"$regex":"C++.*"}})