In my sandbox I have a collection, and the unique key (_id) for the collection is a unique string from another database. I have preallocated the documents and they look like this
The data looks like this
{ _id : "UNIQUEKEY1:1463670000000", data: {value:NaN} }
{ _id : "UNIQUEKEY2:1463670000000", data: {value:NaN} }
I would like to query the data in the following way
{ "_id": {$regex : "/^UNIQUEKEY1.*/i"} }
I have read that you can query _id if it is a string in Brendan's comment here
I don't want the overhead of another attribute just to search by when the _id would provide me with enough
It's a valid setup and $regex should work fine (see https://docs.mongodb.com/manual/reference/operator/query/regex/)
So try db.mycollection.find({ "_id": {$regex : /^UNIQUEKEY1.*/i} }) i.e. you shouldn't need the quote marks.
Related
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 couple of indexes in my Elasticsearch DB as follows
Index_2019_01
Index_2019_02
Index_2019_03
Index_2019_04
.
.
Index_2019_12
Suppose I want to search only on the first 3 Indexes.
I mean a regular expression like this:
select count(*) from Index_2019_0[1-3] where LanguageId="English"
What is the correct way to do that in Elasticsearch?
How can I query several indexes with certain names?
This can be achieved via multi-index search, which is a built-in capability of Elasticsearch. To achieve described behavior one should try a query like this:
POST /index_2019_01,index_2019_02/_search
{
"query": {
"match": {
"LanguageID": "English"
}
}
}
Or, using URI search:
curl 'http://<host>:<port>/index_2019_01,index_2019_02/_search?q=LanguageID:English'
More details are available here. Note that Elasticsearch requires index names to be lowercase.
Can I use a regex to specify index name pattern?
In short, no. It is possible to use index name in queries using a special "virtual" field _index but its use is limited. For instance, one cannot use a regexp against index name:
The _index is exposed as a virtual field — it is not added to the
Lucene index as a real field. This means that you can use the _index
field in a term or terms query (or any query that is rewritten to a
term query, such as the match, query_string or simple_query_string
query), but it does not support prefix, wildcard, regexp, or fuzzy
queries.
For instance, the query from above can be rewritten as:
POST /_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"_index": [
"index_2019_01",
"index_2019_02"
]
}
},
{
"match": {
"LanguageID": "English"
}
}
]
}
}
}
Which employs a bool and a terms queries.
Hope that helps!
Why use POST when you are not adding any additional data to it.
I advise using GET for your case. Secondly, If the Index have similar names like in your case, you should be using an index pattern like in the query below,
GET /index_2019_*/_search
{
"query": {
"match": {
"LanguageID": "English"
}
}
}
OR in a URL
curl -XGET "http://<host>:<port>/index_2019_*/_search" -H 'Content-Type: application/json' -d'{"query": {"match":{"LanguageID": "English"}}}'
While searching for indices using a regex is not possible you might be able to use date math to take you a bit further.
You can look at the docs here
As an example, lets say you wish the last 3 months from those indices
that means that if we have
index_2019_01
index_2019_02
index_2019_03
index_2019_04
And today is 2019/04/20, we could use the following query to get 04,03 and 02
GET /<index-{now/M-0M{yyyy_MM}}>,<index-{now/M-1M{yyyy_MM}}>,<index-{now/M-2M{yyyy_MM}}>
I used M-0M for the first one so the query construction loop doesn't need a special case for the first index
Look at the docs regarding URL encoding this query and how to have literal braces in the index name, if a client is used the URL encoding is done for you (at least in the python client)
I have a complex JSON attribute with the format as below; I have a main part "address" and its sub attribute as present in the format. I'm having a problem using JSON spirit in reading the sub attribute by specifying the read query as address.street_number. Does anyone have any idea about how to solve this?
{
"address" :
{
"street_number" : 5,
"town" : "xxx",
"country" : "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++.*"}})
How to use nin and regex in mongoDB?
I want to find document using nin and regex
but nin does not work!
Query:
{ "$and" : [
{ "id" : { "$nin" : [ "529653cb5bc5b0e42d339bd3" , "529653cb5bc5b0e498339bd3"]}} ,
{ "content" : { "$regex" : "(?i)apple" , "$options" : "i"} }
] }
Should I using mongo subquery?
Your problem could be multiple things depending upon the error you're getting.
But a quick examination of your query suggests it could be your use of the "id" field. The primary key field in all documents is "_id". Your query uses the field "id" but you're probably trying to query the field "_id".