I would like to get all the datas from mongo while i need to replace special characters, but not rewrite the data while the query run:
data in db:
[{
number: "12/34",
type: "string"
},
{
number: "56-78",
type: "string"
},
{
number: "910*2",
type: "string"
}]
the number what I would like to query is: 1234, 5678
the related output is:
[{
number: "12/34",
type: "string"
},
{
number: "56-78",
type: "string"
}]
what I try is to add field temporary without special characters, but I cannot remove all of them because addfield doesn't handle regex. i try to do this with reduce but it's not work for me.
Anybody can help me?
With MongoDB v4.2+, you can use $regexFindAll to locate all digits. Use $reduce and $concat to reconstruct a string with special characters removed/sanitized. Perform the search on the sanitized string.
db.collection.aggregate([
{
"$addFields": {
"sanitized": {
"$regexFindAll": {
"input": "$number",
"regex": "\\d"
}
}
}
},
{
"$addFields": {
"sanitized": {
"$reduce": {
"input": "$sanitized.match",
"initialValue": "",
"in": {
"$concat": [
"$$value",
"$$this"
]
}
}
}
}
},
{
"$match": {
$expr: {
"$in": [
"$sanitized",
[
"1234",
"5678"
]
]
}
}
},
{
"$project": {
sanitized: false
}
}
])
Here is the Mongo playground for your reference.
Related
I have a mongoDB collection with the following objects:
{
name: "a",
email: "aaa#bestemail.com"
},
{
name: "b",
email: "bbb#bestemail123.com"
},
{
name: "c",
email: "ccc#diffemail.com"
}
I want to add to each document a field of true/false if the email address matches the /bestemail/ regex expression.
Meaning, for the current collection I would get:
{
name: "a",
email: "aaa#bestemail.com",
matching: true
},
{
name: "b",
email: "bbb#bestemail123.com",
matching: true
},
{
name: "c",
email: "ccc#diffemail.com",
matching: false
}
The main issue is that I have to work with mongo version 4.0.12 and so I cannot use $regexMatch (available from mongo 4.2)
I have tried using:
$addFields: {"matching": {$cond: [{$in: ["$email", [/bestemail/]]}, true, false]}}
but I get matching: false for all documents.
Thank you for the assistance!
I hesitate to respond since I don't have access to a MongoDB server with version 4.0.12, but perhaps this will work. [I did look at the archived docs.]
db.collection.aggregate([
{
"$addFields": {
"matching": {
"$cond": [
{
"$gt": [
{ "$indexOfCP": [ "$email", "bestemail" ] },
0
]
},
true,
false
]
}
}
}
])
Try it on the wrong server version at mongoplayground.net.
Example output:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"email": "aaa#bestemail.com",
"matching": true,
"name": "a"
},
{
"_id": ObjectId("5a934e000102030405000001"),
"email": "bbb#bestemail123.com",
"matching": true,
"name": "b"
},
{
"_id": ObjectId("5a934e000102030405000002"),
"email": "ccc#diffemail.com",
"matching": false,
"name": "c"
}
]
I have a document like this:
{
"_id": "5ffc130e9fb31b26162e0bad",
"results": [
{
"customer": {
"display_name": "Manno Dispensary - first",
"ext_acct_id": "267"
}
},
{
"customer": {
"display_name": "Manno Dispensary - second",
"ext_acct_id": "262"
}
},
{
"customer": {
"display_name": "Kako Dispensary - first",
"ext_acct_id": "261"
}
},
{
"customer": {
"display_name": "Kako Dispensary - second",
"ext_acct_id": "263"
}
}
]
}
I want to write a MongoDB query which does a regex search on "customer.display_name" and returns all those documents in results that satisfies this criteria.
I have written this query till now, and it returns me the desired output, but the problem is, it is only retuning one document inside results, Am I missing anything in this?
my desired output:
{
"_id": "5ffc130e9fb31b26162e0bad",
"results": [
{
"customer": {
"display_name": "Manno Dispensary - first",
"ext_acct_id": "267"
}
},
{
"customer": {
"display_name": "Manno Dispensary - second",
"ext_acct_id": "262"
}
}
]
}
What I am actually getting:
{
"_id": "5ffc130e9fb31b26162e0bad",
"results": [
{
"customer": {
"display_name": "Manno Dispensary - first",
"ext_acct_id": "267"
}
}
]
}
this is the query which I have written to fetch all customers which contain "Manno" in their customer name.
My collection name is Order(mongoose), search="Manno"
Order.find({
results: {
$elemMatch : {
"customer.display_name": {$regex: search}
}
}
},{
results: {
$elemMatch : {
"customer.display_name": {$regex: search}
}
}
});
The $elemMatch and $ will return only single matching document/object form array, try $filter and $regexMatch,
$filter to iterate loop of results array
$regexMatch to check regular expression condition, it will return true or false
Order.find({
results: {
$elemMatch: {
"customer.display_name": { $regex: "Manno" }
}
}
},
{
results: {
$filter: {
input: "$results",
cond: {
$regexMatch: {
input: "$$this.customer.display_name",
regex: "Manno"
}
}
}
}
})
Playground
My doc looks as follows
doc = {
name: 'abc',
age:20
}
and my query looks like
{ $expr: {$and:[{ $gt:[ "$age", 10 ] },
{ $regex:["$name",'ab']}
]
}
} }
But it's not working and I get an error
Unrecognized expression '$regex'
How can I make it work?
My original query looks like this
db.orders.aggregate([{
$match: {}},
{$lookup: {
from: "orders",
let: {
"customer_details": "$customerDetails"
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $or: [
{
$eq: ["$customerDetails.parentMobile","$$customer_details.parentMobile"]
},
{$eq: ["$customerDetails.studentMobile","$$customer_details.parentMobile"]
},
{$eq: ["$customerDetails.studentMobile","$$customer_details.parentMobile"]
},
{$eq: ["$customerDetails.studentMobile","$$customer_details.studentMobile"]
}
]
},
{$eq: ["$customerDetails.zipCode","$$customer_details.zipCode"]},
{$eq: ["$customerDetails.address","$$customer_details.address"]}
]
}
}
}],
as: "oldOrder"
}
}])
I want to use regex for matching address.
Any help will be greatly appreciated. Thanks in advance.
If your mongoDB version is 4.2, then you can use $regexMatch
try this
db.collection.find({
$expr: {
$and: [
{
$gt: [
"$age",
10
]
},
{
$regexMatch: {
input: "$name",
regex: "ab"
}
}
]
}
})
check this Mongo Playground
$regex is a query operator you cannot use inside $expr because it only supports aggregation pipeline operators.
{
"$expr": { "$gt": ["$age", 10] } ,
"name": { "$regex": "ab" }
}
If you have mongodb 4.2, you can use $regexMatch
{ "$expr": {
"$and": [
{ "$gt": ["$age", 10] },
{
"$regexMatch": {
"input": "$name",
"regex": "ab", //Your text search here
"options": "i",
}
}
]
}}
I have a collection;
"users": [
{
"_id": ObjectId("5c4185be19da7e815cb18f59"),
"name": "User1"
},
{
"_id": ObjectId("5c4185be19da7e815cb18f5a"),
"name": "User2"
} ]
I need to search users collection by regex.
db.results.aggregate([{
"$match": {
"name": {
"$regex": "user",
"$options": "si"
}
}
}
])
this works for searching against user field. I tried with the below code to search against id. But it didn't work for me.
db.results.aggregate([{
"$match": {
"_id": {
"$regex": "18f5a",
"$options": "si"
}
}
}
])
Thanks in advance.
The _id field is ObjectId type by default hence you can't regex match it.
If you're using Mongo version 4.0+ you can use toString.
db.results.aggregate([
{
$addFields: {
_id: {$toString: "$_id"}
}
},
{
"$match": {
"_id": {
"$regex": "18f5a",
"$options": "si"
}
}
}
])
I'm using elasticsearch on AWS to store logs from Cloudfront. I have created a simple query that will give me all entries from the past 24h, sorted from new to old:
{
"from": 0,
"size": 1000,
"query": {
"bool": {
"must": [
{ "match": { "site_name": "some-site" } }
],
"filter": [
{
"range": {
"timestamp": {
"lt": "now",
"gte": "now-1d"
}
}
}
]
}
},
"sort": [
{ "timestamp": { "order": "desc" } }
]
}
Now, there a are certain sources (based on the user agent) for which I would like to exclude results. So my question boils down to this:
How can I filter out entries from the results when a certain field contains a certain string? Or:
query.filter.where('cs_user_agent').does.not.contain('Some string')
(This is not real code, obviously.)
I have tried to make sense of the Elasticsearch documentation, but I couldn't find a good example of how to achieve this.
I hope this makes sense. Thanks in advance!
Okay, I figured it out. What I've done is use a Bool Query in combination with a wildcard:
{
"from": 0,
"size": 1000,
"query": {
"bool": {
"must": [
{ "match": { "site_name": "some-site" } }
],
"filter": [
{
"range": {
"timestamp": {
"lt": "now",
"gte": "now-1d"
}
}
}
],
"must_not": [
{ "wildcard": { "cs_user_agent": "some string*" } }
]
}
},
"sort": [
{ "timestamp": { "order": "desc" } }
]
}
This basically matches any user agent string containing "some string", and then filters it out (because of the "must_not").
I hope this helps others who run into this problem.
nod.js client version:
const { from, size, value, tagsIdExclude } = req.body;
const { body } = await elasticWrapper.client.search({
index: ElasticIndexs.Tags,
body: {
from: from,
size: size,
query: {
bool: {
must: {
wildcard: {
name: {
value: `*${value}*`,
boost: 1.0,
rewrite: 'constant_score',
},
},
},
filter: {
bool: {
must_not: [
{
terms: {
id: tagsIdExclude ? tagsIdExclude : [],
},
},
],
},
},
},
},
},
});