MongoDB Shell使用

    科技2026-09-04  8

    MongoDB Compass的启动:

    mongodb-compass

    单击MongoDB Compass下方的 MongoSH Beta,从而启动MongoDB Shell: 当使用MongoDB Shell时,默认连接到名为test的数据库,如果需要切换不同的数据库,使用以下命令:

    use <database name>

    如果需要进行查找,如下,AutoSPIResult是collection:

    db.AutoSPIResult.find( {"TestResult":"pass"} )

    如果需要同时查找多个条件则用,连接即可:

    db.AutoSPIResult.find({"TestResult":"pass","LineName":"12345"})

    如果需要查找的是Array里的元素,则用.连接,这里DetailInfo是Array:

    db.AutoSPIResult.find({"DetailInfo.Component":"Q5870"})

    常用命令之mongodb 查询chunks文件内容:

    db.getCollection('fs.chunks').find({"字段":"内容"})

    查询所有:

    db.getCollection('fs.chunks').find({})

    统计数量

    db.getCollection('fs.chunks').find({}).count()

    根据时间排序:

    db.getCollection('fs.files').find({}).sort({'uploadDate':-1})

    根据_id查询:

    db.getCollection('fs.files').find({'_id':ObjectId("5f165b70eece4b030cf9ee4f")})

    根据某个字段模糊查询:

    db.getCollection('fs.files').find({'filename':/^.*demo.*$/}) db.getCollection('fs.files').find({}).size() db.getCollection('fs.files').find({'filename':/^.*demo.*$/}).size()

    根据.files文件查询结果查询.chunks文件

    db.getCollection("SPIResult.files").find({"metadata.SN":"FV9021423QJMYD5BH"}) db.getCollection("SPIResult.chunks").find({"files_id":ObjectId("5ec62c0419d9843f4cb76a32")})

    参考链接: MongoDB Compass的安装:Download and Install Compass Embedded MongoDB Shell:Embedded MongoDB Shell 官方Query 语法:Query Documents db.collection.find()方法

    Processed: 0.010, SQL: 9