如何查询对象数组中的正则表达式并将结果切片到Mongodb?
How to query Regular Expression in an Array of object and slice the result in Mongodb?
我有这样的文件..
"auction_list": [
{
"auction_id": 11368494,
"domain": "51love.cn",
"utf_name": "51love.cn",
"is_idn": false,
"auction_type": "backorder",
"currency": "USD",
"current_bid_price": "36.00",
"bids": 25,
"bidders": 4,
"time_left": "4 days, 5 hours",
"start_time": "2021/06/15 14:30 PST",
"start_time_stamp": 1623792604841,
"end_time": "2021/06/22 03:02 PST",
"end_time_stamp": 1624356147000,
"estibot_appraisal": "[=11=].00"
},
{
"auction_id": 11381539,
"domain": "meiguihualove.cn",
"utf_name": "meiguihua.cn",
"is_idn": false,
"auction_type": "backorder",
"currency": "USD",
"current_bid_price": "15.99",
"bids": 5,
"bidders": 4,
"time_left": "5 days, 5 hours",
"start_time": "2021/06/16 14:30 PST",
"start_time_stamp": 1623879010264,
"end_time": "2021/06/23 03:02 PST",
"end_time_stamp": 1624442573000,
"estibot_appraisal": "[=11=].00"
},
{
"auction_id": 11273186,
"domain": "surpass.cn",
"utf_name": "surpass.cn",
"is_idn": false,
"auction_type": "backorder",
"currency": "USD",
"current_bid_price": "14.99",
"bids": 4,
"bidders": 4,
"time_left": "1 day, 5 hours",
"start_time": "2021/06/09 14:30 PST",
"start_time_stamp": 1623274205156,
"end_time": "2021/06/19 03:02 PST",
"end_time_stamp": 1624096958000,
"estibot_appraisal": ".00"
}
我在那个数组中总共有 30000 个对象。
如果域词包含 love (使用正则表达式),我想搜索该域。并想将结果从第 1 项切片到第 50 项,如果找到,则将它们切片到第 50 到 100 项?
我试过了
ExpiredDomains.find({auction_list:{$elemMatch:{domain:'/love/'}}})
和
ExpiredDomains.find({auction_list:{$elemMatch:{domain:{$regex: 'love'}}})
但这return没什么。
我怎样才能做到这一点?我正在使用节点 js。
提前致谢
以下聚合可以使用正则表达式过滤所有记录。
db.collection.aggregate([
{
"$project": {
"auction_list": {
"$filter": {
"input": "$auction_list",
"as": "list",
cond: {
"$regexMatch": {
"input": "$$list.domain",
"regex": /love/
}
}
}
}
}
},
{
"$unwind": "$auction_list"
},
{
"$skip": 0
},
{
"$limit": 50
}
])
编辑:用于限制和跳过。
分配两个不同的变量:
const a = 0;
const b = 50;
//And use it like
{
"$skip": a
},
{
"$limit": b
}
并动态更改它们。跳过将忽略第一个 x
记录。如果你给 skip = 50
limit = 100
你将获得第二个 50 条记录。
我有这样的文件..
"auction_list": [
{
"auction_id": 11368494,
"domain": "51love.cn",
"utf_name": "51love.cn",
"is_idn": false,
"auction_type": "backorder",
"currency": "USD",
"current_bid_price": "36.00",
"bids": 25,
"bidders": 4,
"time_left": "4 days, 5 hours",
"start_time": "2021/06/15 14:30 PST",
"start_time_stamp": 1623792604841,
"end_time": "2021/06/22 03:02 PST",
"end_time_stamp": 1624356147000,
"estibot_appraisal": "[=11=].00"
},
{
"auction_id": 11381539,
"domain": "meiguihualove.cn",
"utf_name": "meiguihua.cn",
"is_idn": false,
"auction_type": "backorder",
"currency": "USD",
"current_bid_price": "15.99",
"bids": 5,
"bidders": 4,
"time_left": "5 days, 5 hours",
"start_time": "2021/06/16 14:30 PST",
"start_time_stamp": 1623879010264,
"end_time": "2021/06/23 03:02 PST",
"end_time_stamp": 1624442573000,
"estibot_appraisal": "[=11=].00"
},
{
"auction_id": 11273186,
"domain": "surpass.cn",
"utf_name": "surpass.cn",
"is_idn": false,
"auction_type": "backorder",
"currency": "USD",
"current_bid_price": "14.99",
"bids": 4,
"bidders": 4,
"time_left": "1 day, 5 hours",
"start_time": "2021/06/09 14:30 PST",
"start_time_stamp": 1623274205156,
"end_time": "2021/06/19 03:02 PST",
"end_time_stamp": 1624096958000,
"estibot_appraisal": ".00"
}
我在那个数组中总共有 30000 个对象。 如果域词包含 love (使用正则表达式),我想搜索该域。并想将结果从第 1 项切片到第 50 项,如果找到,则将它们切片到第 50 到 100 项?
我试过了
ExpiredDomains.find({auction_list:{$elemMatch:{domain:'/love/'}}})
和
ExpiredDomains.find({auction_list:{$elemMatch:{domain:{$regex: 'love'}}})
但这return没什么。 我怎样才能做到这一点?我正在使用节点 js。 提前致谢
以下聚合可以使用正则表达式过滤所有记录。
db.collection.aggregate([
{
"$project": {
"auction_list": {
"$filter": {
"input": "$auction_list",
"as": "list",
cond: {
"$regexMatch": {
"input": "$$list.domain",
"regex": /love/
}
}
}
}
}
},
{
"$unwind": "$auction_list"
},
{
"$skip": 0
},
{
"$limit": 50
}
])
编辑:用于限制和跳过。
分配两个不同的变量:
const a = 0;
const b = 50;
//And use it like
{
"$skip": a
},
{
"$limit": b
}
并动态更改它们。跳过将忽略第一个 x
记录。如果你给 skip = 50
limit = 100
你将获得第二个 50 条记录。