db.getCollection('ldy_cve_v5').find({ _id: ObjectId("65dea60ad8bcde3b94d82351") })
某个字段不为空
db.getCollection('ldy_cve_v5').find({"containers.cna.cnnvdLevel": {"$exists": true, "$ne": null}})
删除某条
db.getCollection('ldy_cve_v5').deleteMany({
$or: [
{ 'cveMetadata.cveId': { $exists: false } },
{ 'cveMetadata.cveId': { $eq: "" } }
]
})
# 创建唯一索引:
db.ldy_cve_v5_cwe.createIndex( { "cweId":"" }, { unique: true } )
db.getCollection('ldy_cve_v5').find({
$or: [
{ 'ldyMetadata.ldyId': 'LDY-2023-00186623' },
]
})
# 查询情报
db.getCollection('ldy_cve_v5').find({
$and: [
{ 'qingBao.isPush': 1 },
{ 'ldyLDMetadata': { $not: { $exists: true } } }
]
})
db.getCollection('ldy_cve_v5').find({
$and: [
{ 'containers.cna.isQbAsync': 1 },
{ 'qingBao.isPush': { $exists: false }},
]
})
# 设置值为0
db.getCollection('ldy_cve_v5').updateMany(
{
$and: [
{ 'containers.cna.isQbAsync': 1 },
{ 'qingBao.isPush': { $exists: false } }
]
},
{ $set: { 'qingBao.isPush': 0 } }
)
// 删除某个字段
db.getCollection('ldy_cve_v5').updateMany({}, {$unset: {qingBao: ""}})
db.getCollection('ldy_cve_v5').find({
$or: [
{ 'ldyMetadata.ldyId': { $regex: "LDY-2023-00186623", $options: "i" } },
]
})
db.getCollection('ldy_cve_v5').find({
$or: [
{ 'cveMetadata.cveId': 'CVE-2023-0423' },
]
})
db.getCollection('ldy_cve_v5').find({
'cveMetadata.cveId': { $in: ['CVE-2023-0423', 'CVE-2023-0424'] }
})
db.getCollection('ldy_cve_v5_capec').find({
descriptions: {
$elemMatch: {
lang: "cn",
description: { $regex: "script", $options: "i" }
}
}
})
# 某字段存在,不为空
db.getCollection('ldy_cve_v5').find({ "srcMetadata.srcId": { $exists: true, $ne: null } })
# 某字段不存在或者为空
db.getCollection('ldy_cve_v5').find({
$or: [
{ "isOpenTime": { $exists: false } }, // 查询字段不存在的文档
{ "isOpenTime": { $eq: "" } } // 查询字段存在且为空的文档
]
})
# 查询数组不为空
db.getCollection('ldy_cve_v5').find({ "containers.cna.tags360": { $exists: true, $not: {$size: 0} } }).size()
# 删除所有标签
db.getCollection('ldy_cve_v5').updateMany(
{ },
{ "$set": { "containers.cna.tags360": [] } }
)
db.getCollection('ldy_cve_v5').updateMany(
{ },
{ "$set": { "containers.cna.exps": [] } }
)
db.getCollection('ldy_cve_v5').updateMany(
{ },
{ "$set": { "containers.cna.patchs": [] } }
)
# 查询数据包含某个元素
db.getCollection('ldy_cve_v5').find({ "containers.cna.metrics": { $elemMatch: { "cvssV2_0": {$exists: true} } } })
# 正则
db.getCollection('ldy_cve_v5').find({
$and: [
{"cveMetadata.datePublished": {$regex: "^2023"}},
{
$or: [
{"containers.cna.metrics.cvssV3_1": {$exists: true}},
{"containers.cna.metrics.cvssV3_0": {$exists: true}},
{"containers.cna.metrics.cvssV2_0": {$exists: true}}
]
}
]
})
db.getCollection('ldy_cve_v5').find({
$and: [
{
"cveMetadata.datePublished": {
$regex: "^2023"
}
},
{
$or: [
{
"containers.cna.metrics.cvssV3_1": {
$exists: true
}
},
{
"containers.cna.metrics.cvssV3_0": {
$exists: true
}
},
{
"containers.cna.metrics.cvssV2_0": {
$exists: true
}
}
]
},
{
"containers.cna.tags360": {
$not: {
$elemMatch: {
"tagId": {
$in: [
"CpCS8X/55Ts=",
"Uv7xjKkYkkE=",
"5YZzU4ZPhiY="
]
}
}
}
}
}
]
})
# 这个查询会找到所有具有非空字段的文档。 $exists: true 确保字段存在,并且 $ne: [] 则确保该字段不是空数组。这样可以过滤出数组字段不为空的文档。
db.getCollection('ldy_cve_v5').find({
$and:[
{"containers.cna.effects": { $exists: true, $ne: [] } },
{"bugtraqMetadata":{$exists: true}}
]
})
共有 0 - MongoDB 条件操作符和demo