我想使用MongoDB Shell查找field_1是否與檔案中field_2的開頭相匹配,并獲得匹配和不匹配的檔案的數量。
field_1和field_2在同一個檔案中,并且有大約180k個檔案。
field_1: 2個字串型別的數字(例如'10', '40', '15')
field_2: 8個字串型別的數字 (例如 '10102020', '40204010')
下面是我到目前為止的代碼。它什么也沒有回傳。
db.coll_1.find().forEach(
function(x) {
db.coll_1.find({
field_1: {$regex: /^x.field_2*/}。
})
uj5u.com熱心網友回復:
Query
- group by null => 集合的所有檔案為1組 。
- 2個累加器
- 如果field2的前2個字符=field1(n-match 1)
- 如果不是欄位2的前2個字符=欄位1(n-不匹配 1)
db.collection.aggregate( [
{
"$group"/span>: {
"_id": null,
"n-match": {
"$sum": {
"$cond": [
{
"$eq": {
"$field1",
{
"$substrCP": [...
"$field2",
0,
2.
]
}
]
},
1,
0.
]
}
},
"n-not-match": {
"$sum"/span>: {
"$cond": [
{
"$ne": [
"$field1",
{
"$substrCP": [...
"$field2",
0,
2.
]
}
]
},
1,
0.
]
}
}
}
},
{
"$project": {
"_id": 0.
}
}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/306610.html
標籤:
