English 中文(简体)
mongo 查询中的匹配主机列表不工作
原标题:Match list of host in mongo query doesn t work
I have below query : mydb.my-collection.aggregate([ { $match: { $and: [ {timestamp: { $gte: $__timeFrom, $lte: $__timeTo }} ] } }, { $group: { _id: { host: "$host" }, sum: {$sum: "$status"}, count: {$count: {}} } }, { $project: { price: {$divide: ["$sum", "$count"]} } } ]) I am trying to match only specific hosts, so i updated the query like below: mydb.my-collection.aggregate([ { $match: { host : {$regex: ".*SER1.*"}, host: {$regex: ".*SER2.*"}, host: {$regex: ".*SER3.*"} $and: [ {timestamp: { $gte: $__timeFrom, $lte: $__timeTo }} ] } }, { $group: { _id: { host: "$host" }, sum: {$sum: "$status"}, count: {$count: {}} } }, { $project: { price: {$divide: ["$sum", "$count"]} } } ]) I also tried {host: { $in:["SER1","SER2","SER3"] }} for matching the host. But my result shows all other hosts, i only want to show SER1, SER2, SER3. Is there any way to use list or array of values which i want to match?
问题回答
You can t have the same field name in the object. You should work with $expr in order to use the $regexMatch operator. To match any regex pattern, you should wrap the multiple (regex) conditions in the $or operator. Also, you need to modify the query for comparing the timestamp field as applying the $expr. { $match: { $expr: { $and: [ { $or: [ { $eq: [ { $regexMatch: { input: "$host", regex: ".*SER1.*" } }, true ] }, { $eq: [ { $regexMatch: { input: "$host", regex: ".*SER2.*" } }, true ] }, { $eq: [ { $regexMatch: { input: "$host", regex: ".*SER3.*" } }, true ] } ] }, { $and: [ { $gte: [ "$timestamp", __timeFrom ] }, { $lte: [ "$timestamp", __timeTo ] } ] } ] } } } Another approach is combining the regexes into non-capturing groups ((?:)) and alternatives (|). This will make the query shorter. Regex 101 { $match: { host: { $regex: "(?:.*SER1.*)|(?:.*SER2.*)|(?:.*SER3.*)" }, $and: [ { timestamp: { $gte: __timeFrom, $lte: __timeTo } } ] } }




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签