Anda bisa melakukan ini dengan kerangka kerja agregasi 2.2. Sesuatu seperti ini;
db.books.runCommand("aggregate", {
pipeline: [
{ // find docs that contain Par*
$match: { "indexTokens" : { "$regex" : "^Par" , "$options" : "i"}},
},
{ // create a doc with a single array elemm for each indexToken entry
$unwind: "$indexTokens"
},
{ // now produce a list of index tokens
$group: {
_id: "$indexTokens",
},
},
],
})
Atau ini mungkin lebih dekat dengan apa yang Anda cari jika Anda benar-benar menginginkan array tanpa dokumen;
db.books.runCommand("aggregate", {
pipeline: [
{ // find docs that contain Par*
$match: { "indexTokens" : { "$regex" : "^Par" , "$options" : "i"}},
},
{ // create a doc with a single array elemm for each indexToken entry
$unwind: "$indexTokens"
},
{ // now throw out any unwind's that DON'T contain Par*
$match: { "indexTokens": { "$regex": "^Par", "$options": "i" } },
},
{ // now produce the list of index tokens
$group: {
_id: null,
indexTokens: { $push: "$indexTokens" },
},
},
],
})