目录

要通过命令行操作MongoDB,你可以使用mongo命令与MongoDB实例进行交互。以下是一些常用的命令行操作:

  1. 连接到MongoDB实例:在终端中输入以下命令连接到MongoDB实例(默认连接本地)。
mongo

如果MongoDB实例不在本地,可以指定带有主机和端口号的连接字符串。

mongo mongodb://hostname:port
(env) [root@VM-16-17-opencloudos mongo_test]# mongo
MongoDB shell version v4.4.22
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ec38a30e-b967-4ebb-8f85-adb430dd677f") }
MongoDB server version: 4.4.22
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2023-07-06T10:20:25.916+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2023-07-06T10:20:26.632+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2023-07-06T10:20:26.633+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---

show

    show databases
    
    1. 切换到特定数据库:使用以下命令切换到一个特定的数据库。
    1. 列出当前数据库的所有集合:在数据库中,可以使用以下命令列出所有集合。
    show collections
    
    > show databases;
    admin       0.000GB
    config      0.000GB
    local       0.000GB
    mydatabase  0.000GB
    > use mydatabase
    switched to db mydatabase
    > show collections
    mycollection
    

    CRUD

    插入文档到集合:使用以下语法将文档插入到指定集合中。

    db.collection_name.insertOne({"key": "value"})
    
    > db.mycollection.insertOne({"key": "value"})
    {
            "acknowledged" : true,
            "insertedId" : ObjectId("64a62a162b45c0f74eddc97e")
    }
    

    查询集合中的文档:使用以下语法查询集合中的文档。

    db.collection_name.find()
    
    > db.mycollection.find()
    { "_id" : ObjectId("64a628ea3e7b15e12ea38f7d"), "name" : "Emma", "age" : 28, "city" : "London" }
    { "_id" : ObjectId("64a628ea3e7b15e12ea38f7e"), "name" : "Michael", "age" : 35, "city" : "Tokyo" }
    { "_id" : ObjectId("64a62906f1b8c3e7534a4d48"), "name" : "John", "age" : 30, "city" : "New York" }
    { "_id" : ObjectId("64a62906f1b8c3e7534a4d49"), "name" : "Emma", "age" : 28, "city" : "London" }
    { "_id" : ObjectId("64a62906f1b8c3e7534a4d4a"), "name" : "Michael", "age" : 35, "city" : "Tokyo" }
    { "_id" : ObjectId("64a62a162b45c0f74eddc97e"), "key" : "value" }
    > 
    

    更新集合中的文档:使用以下语法更新集合中的文档。

    db.collection_name.updateOne({"key": "value"}, {"$set": {"new_key": "new_value"}})
    
    > db.mycollection.updateOne({"key": "value"}, {"$set": {"new_key": "new_value"}})
    { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
    > db.mycollection.find()
    { "_id" : ObjectId("64a628ea3e7b15e12ea38f7d"), "name" : "Emma", "age" : 28, "city" : "London" }
    { "_id" : ObjectId("64a628ea3e7b15e12ea38f7e"), "name" : "Michael", "age" : 35, "city" : "Tokyo" }
    { "_id" : ObjectId("64a62906f1b8c3e7534a4d48"), "name" : "John", "age" : 30, "city" : "New York" }
    { "_id" : ObjectId("64a62906f1b8c3e7534a4d49"), "name" : "Emma", "age" : 28, "city" : "London" }
    { "_id" : ObjectId("64a62906f1b8c3e7534a4d4a"), "name" : "Michael", "age" : 35, "city" : "Tokyo" }
    { "_id" : ObjectId("64a62a162b45c0f74eddc97e"), "key" : "value", "new_key" : "new_value" }
    

    删除集合中的文档:使用以下语法删除集合中的文档。

    db.collection_name.deleteOne({"key": "value"})
    
    > db.mycollection.find()
    { "_id" : ObjectId("64a628ea3e7b15e12ea38f7d"), "name" : "Emma", "age" : 28, "city" : "London" }
    { "_id" : ObjectId("64a628ea3e7b15e12ea38f7e"), "name" : "Michael", "age" : 35, "city" : "Tokyo" }
    { "_id" : ObjectId("64a62906f1b8c3e7534a4d48"), "name" : "John", "age" : 30, "city" : "New York" }
    { "_id" : ObjectId("64a62906f1b8c3e7534a4d49"), "name" : "Emma", "age" : 28, "city" : "London" }
    { "_id" : ObjectId("64a62906f1b8c3e7534a4d4a"), "name" : "Michael", "age" : 35, "city" : "Tokyo" }
    >