首页 Mongdb数据库教程 MongoDB文档创建、更新和删除命令使用演示
pay pay
教程目录

MongoDB文档创建、更新和删除命令使用演示

日期: 四月 10, 2023, 4:30 p.m.
阅读: 162
作者: Python自学网-村长

摘要: MongoDB文档创建、更新和删除命令使用演示

一、MongoDB创建文档

MongoDB中,可以使用以下三种方法来插入文档:

1.使用mongo shell

在MongoDB的shell界面中,使用以下命令来插入文档:

use mydatabase
db.mycollection.insertOne({"name": "John", "age": 30})

mydatabase是要插入文档的数据库名称,mycollection是要插入文档的集合名称。insertOne()方法用于向集合中插入一个文档。文档以JSON格式表示,可以包含任何字段和值。

2.使用命令行工具

在命令行中,使用以下命令来插入文档:

mongo mydatabase --eval "db.mycollection.insertOne({'name': 'John', 'age': 30})"

mydatabase是要插入文档的数据库名称,mycollection是要插入文档的集合名称。如果集合不存在,则MongoDB将自动创建它。

3.使用驱动程序API

如果您正在使用MongoDB的驱动程序API来访问MongoDB数据库,则可以使用以下代码来插入文档:

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/mydatabase';

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  const dbo = db.db("mydatabase");
  const myobj = { name: "John", age: 30 };
  dbo.collection("mycollection").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log("1 document inserted");
    db.close();
  });
});

其中,mydatabase是要插入文档的数据库名称,mycollection是要插入文档的集合名称,url是MongoDB的连接字符串。在连接成功后,您可以调用db.collection().insertOne()方法来插入文档。

以上是MongoDB插入文档的三种方法。无论使用哪种方法,只要MongoDB连接成功,您就可以向集合中插入新的文档。

二、 MongoDB 更新文档

可以使用以下三种方法来更新文档:

1.使用mongo shell

在MongoDB的shell界面中,使用以下命令来更新文档:

use mydatabase
db.mycollection.updateOne(
   { "name": "John" },
   { $set: { "age": 35 } }
)

其中,mydatabase是要更新文档的数据库名称,mycollection是要更新文档的集合名称。updateOne()方法用于更新满足条件的第一个文档。在本例中,将会将姓名为"John"的文档的年龄更新为35。

2.使用命令行工具

在命令行中,使用以下命令来更新文档:

mongo mydatabase --eval "db.mycollection.updateOne({'name': 'John'}, {'$set': {'age': 35}})"

其中,mydatabase是要更新文档的数据库名称,mycollection是要更新文档的集合名称。

3.使用驱动程序API

如果您正在使用MongoDB的驱动程序API来访问MongoDB数据库,则可以使用以下代码来更新文档:

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/mydatabase';

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  const dbo = db.db("mydatabase");
  const myquery = { name: "John" };
  const newvalues = { $set: { age: 35 } };
  dbo.collection("mycollection").updateOne(myquery, newvalues, function(err, res) {
    if (err) throw err;
    console.log("1 document updated");
    db.close();
  });
});

mydatabase是要更新文档的数据库名称,mycollection是要更新文档的集合名称,url是MongoDB的连接字符串。在连接成功后,您可以调用db.collection().updateOne()方法来更新文档。

以上是MongoDB更新文档的三种方法。无论使用哪种方法,只要MongoDB连接成功,并且找到了需要更新的文档,就可以更新该文档的字段和值。

三、 MongoDB 删除文档

可以使用以下三种方法来删除文档:

1.使用mongo shell

在MongoDB的shell界面中,使用以下命令来删除文档:

use mydatabase
db.mycollection.deleteOne({ "name": "John" })

mydatabase是要删除文档的数据库名称,mycollection是要删除文档的集合名称。deleteOne()方法用于删除满足条件的第一个文档。在本例中,将会删除姓名为"John"的第一个文档。

2.使用命令行工具

在命令行中,使用以下命令来删除文档:

mongo mydatabase --eval "db.mycollection.deleteOne({'name': 'John'})"

mydatabase是要删除文档的数据库名称,mycollection是要删除文档的集合名称。

3.使用驱动程序API

如果您正在使用MongoDB的驱动程序API来访问MongoDB数据库,则可以使用以下代码来删除文档:

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/mydatabase';

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  const dbo = db.db("mydatabase");
  const myquery = { name: "John" };
  dbo.collection("mycollection").deleteOne(myquery, function(err, obj) {
    if (err) throw err;
    console.log("1 document deleted");
    db.close();
  });
});

mydatabase是要删除文档的数据库名称,mycollection是要删除文档的集合名称,url是MongoDB的连接字符串。在连接成功后,您可以调用db.collection().deleteOne()方法来删除文档。

以上是MongoDB删除文档的三种方法。无论使用哪种方法,只要MongoDB连接成功,并且找到了需要删除的文档,就可以将该文档从集合中删除。

部分文字内容为【Python自学网】原创作品,转载请注明出处!视频内容已申请版权,切勿转载!
回顶部