我有這個貓鼬模式,我想以字串陣列的形式查詢所有 IP 屬性。 像 [IP,IP,IP,IP] 這個,而不是 [ { ip : ip} ]
const proxyIpSchema = new Schema<IProxyIp>({
ip: {
type: String,
required: true,
//ignore duplicate ip
unique: true,
},
port: {
type: Number,
required: false,
default: null
},
reason: {
type: String,
required: true,
default: 'Unknown Detection.'
}
},
{
timestamps: true,
}
);
我不能使用地圖功能,因為它會吃掉后端處理能力。像這樣我想要所有的ips作為一個字串陣列
// 從 mongo db 獲取所有 ips 并推送到 redis
await ProxyIp.find({}, { ip: 1 }).then(async (docs) => {
docs.map(async (doc) => {
await this.RedisClient?.sAdd(redisTable, doc.ip);
});
}).catch((err) => {
});
uj5u.com熱心網友回復:
這是您可以將所有"ip"s 放入單個陣列(在物件內)的一種方法。
db.ProxyIp.aggregate([
{
"$group": {
"_id": null,
"ips": {
"$push": "$ip"
}
}
},
{
"$unset": "_id"
}
])
示例輸出:
[
{
"ips": [
"102.118.108.76",
"34.234.240.83",
"123.76.73.33",
"134.81.197.85",
"193.122.45.195",
"54.25.18.14",
"185.68.124.193",
"3.105.130.68",
"52.72.204.78",
"117.212.118.167",
"199.155.140.226",
"64.194.68.59",
"57.4.147.57",
"190.116.4.243",
"179.111.74.98",
...
]
}
[
在mongoplayground.net上試試。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/532494.html
