MongoDB 简介 - 记录和值

发布日期:2026-06-25 06:05:06   来源 : 杭州电子商务研究院    浏览量 :19
杭州电子商务研究院 发布日期:2026-06-25 06:05:06  
19

开始设置

请阅读本系列的第一个指南,以获取有关设置 MongoDB 的更多信息 -安装、Shell 和数据库管理

批量插入

是的,你没看错。与 MySQL 一样,你也可以在一个时间点插入多条记录。同样的insert()函数不仅接受对象,还接受数组作为输入。这次,我们将偷懒,为接下来的两条记录创建一些简单的东西。

      db.students.insert([
  {
    name: "Purushothaman",
    degree: "Management",
    email: "purush@example.com"
  },
  {
    name: "Meaow Meaow",
    degree: "Cat Study",
    email: "meaow@example.com",
    phone: ["9850420420"]
  }
]);
    

您可以看到,上述记录都是不同的。这让我们看到了文档数据库的最大优势。您不需要为记录设置固定的架构,并且每条记录都可以是任何有效的 JavaScript 表达式。

“动态”更改模式是使用 NoSQL 的最大优势。

现在,让我们尝试在数据库上执行上述插入查询。

      > db.students.insert(
...   [
...     {
...       "name": "Purushothaman",
...       "degree": "Management",
...       "email": "purush@example.com"
...     },
...     {
...       "name": "Meaow Meaow",
...       "degree": "Cat Study",
...       "email": "meaow@example.com",
...       "phone": ["9850420420"]
...     },
...   ]
... );
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 2,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
>
    

您将获得BulkWriteResult函数的输出,其中没有显示任何错误,并且插入的记录数为“2”。让我们使用find()方法快速检查表内容。

      > db.students.find();
{ "_id" : ObjectId("592ebe7e8e61243307417cc4"), "name" : "Praveen Kumar", "degree" : "Cloud Computing", "email" : "praveen@example.com", "subjects" : [ { "name" : "Internet Networks", "prof" : "Prof. Awesome Blossom" }, { "name" : "Cloud Computing", "prof" : "Prof. Tech Ninja" }, { "name" : "Web Development", "prof" : "Prof. Chunky Monkey" } ], "phone" : [ "9840035007", "9967728336", "7772844242" ] }
{ "_id" : ObjectId("592ed5818e61243307417cc5"), "name" : "Purushothaman", "degree" : "Management", "email" : "purush@example.com" }
{ "_id" : ObjectId("592ed5818e61243307417cc6"), "name" : "Meaow Meaow", "degree" : "Cat Study", "email" : "meaow@example.com", "phone" : [ "9850420420" ] }
>
    

我们得到了三条记录,每条记录都有不同的架构,这很棒!由于字段太多,让我们将其改为pretty()

      > db.students.find().pretty();
{
    "_id" : ObjectId("592ebe7e8e61243307417cc4"),
    "name" : "Praveen Kumar",
    "degree" : "Cloud Computing",
    "email" : "praveen@example.com",
    "subjects" : [
        {
            "name" : "Internet Networks",
            "prof" : "Prof. Awesome Blossom"
        },
        {
            "name" : "Cloud Computing",
            "prof" : "Prof. Tech Ninja"
        },
        {
            "name" : "Web Development",
            "prof" : "Prof. Chunky Monkey"
        }
    ],
    "phone" : [
        "9840035007",
        "9967728336",
        "7772844242"
    ]
}
{
    "_id" : ObjectId("592ed5818e61243307417cc5"),
    "name" : "Purushothaman",
    "degree" : "Management",
    "email" : "purush@example.com"
}
{
    "_id" : ObjectId("592ed5818e61243307417cc6"),
    "name" : "Meaow Meaow",
    "degree" : "Cat Study",
    "email" : "meaow@example.com",
    "phone" : [
        "9850420420"
    ]
}
>
    

啊!就这样!

查询记录

为了查询或过滤字段,我们需要将它们作为参数对象传递给 find ()函数。举个例子,让我们看看我是否能用Meaow Meaow获取记录

      > db.students.find({"name" : "Meaow Meaow"});
{ "_id" : ObjectId("592ed5818e61243307417cc6"), "name" : "Meaow Meaow", "degree" : "Cat Study", "email" : "meaow@example.com", "phone" : [ "9850420420" ] }
>
    

和往常一样,我们的pretty()会返回:

      > db.students.find({"name" : "Meaow Meaow"}).pretty();
{
    "_id" : ObjectId("592ed5818e61243307417cc6"),
    "name" : "Meaow Meaow",
    "degree" : "Cat Study",
    "email" : "meaow@example.com",
    "phone" : [
        "9850420420"
    ]
}
>
    

这也适用于数组中的项。find()方法在此处对值进行精确匹配。以下是另一个在数组中匹配数字的示例:

      > db.students.find({"phone": "9840035007"}).pretty();
{
    "_id" : ObjectId("592ebe7e8e61243307417cc4"),
    "name" : "Praveen Kumar",
    "degree" : "Cloud Computing",
    "email" : "praveen@example.com",
    "subjects" : [
        {
            "name" : "Internet Networks",
            "prof" : "Prof. Awesome Blossom"
        },
        {
            "name" : "Cloud Computing",
            "prof" : "Prof. Tech Ninja"
        },
        {
            "name" : "Web Development",
            "prof" : "Prof. Chunky Monkey"
        }
    ],
    "phone" : [
        "9840035007",
        "9967728336",
        "7772844242"
    ]
}
>
    

尽管9840035007只是现有的三个数字之一,上述命令仍能正确识别我的记录并显示它。

更新记录

这里我们使用的方法是db.collection.update()。它接受两个参数,第一个参数是记录中存在的匹配项的键值对对象。下一个参数是第一个参数应该替换的内容。

因此让我们尝试这样的事情:

      db.students.update(
  { name: "Praveen Kumar" },
  {
    name: "Praveen Kumar",
    degree: "Cloud Computing MSc",
    email: "praveen@example.net",
    subjects: [
      {
        name: "Internet Networks",
        prof: "Prof. Awesome Blossom"
      },
      {
        name: "Cloud Computing",
        prof: "Prof. Tech Ninja"
      },
      {
        name: "Web Development",
        prof: "Prof. Chunky Monkey"
      }
    ],
    phone: ["9840035007", "9967728336", "7772844242"]
  }
);
    

注意 1:如果使用更新函数,则需要提供完整对象,因为它会用第二个参数替换整个记录。如果我们没有设置其他值,它将只有一条记录,其中只有两个项目:电子邮件和学位。

注意 2:尽可能不要像我刚才那样按名称查找。可能有许多记录与同一参数匹配,因此请使用诸如_id之类的唯一名称。

尝试上述方法,我们得到:

      > db.students.update({"name": "Praveen Kumar"}, {
...   "name": "Praveen Kumar",
...   "degree": "Cloud Computing MSc",
...   "email": "praveen@example.net",
...   "subjects": [
...     {
...       "name": "Internet Networks",
...       "prof": "Prof. Awesome Blossom"
...     },
...     {
...       "name": "Cloud Computing",
...       "prof": "Prof. Tech Ninja"
...     },
...     {
...       "name": "Web Development",
...       "prof": "Prof. Chunky Monkey"
...     }
...   ],
...   "phone": ["9840035007", "9967728336", "7772844242"]
... });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
    

瞧!让我们看看结果:

      {
  "nMatched": 1,
  "nUpserted": 0,
  "nModified": 1
}
    

它匹配了一条记录并对其进行了修改。没有更新记录(我们稍后会介绍)。这意味着它有可能匹配,但不会更新。让我们再次运行相同的命令,看看会发生什么。

哦天哪!输出结果是一样的。也许我太热情了。

要查看发生了什么变化,我们可以尝试运行find()函数以及 pretty ()函数。

      > db.students.find({"phone": "9840035007"}).pretty();
{
    "_id" : ObjectId("592ebe7e8e61243307417cc4"),
    "name" : "Praveen Kumar",
    "degree" : "Cloud Computing MSc",
    "email" : "praveen@example.net",
    "subjects" : [
        {
            "name" : "Internet Networks",
            "prof" : "Prof. Awesome Blossom"
        },
        {
            "name" : "Cloud Computing",
            "prof" : "Prof. Tech Ninja"
        },
        {
            "name" : "Web Development",
            "prof" : "Prof. Chunky Monkey"
        }
    ],
    "phone" : [
        "9840035007",
        "9967728336",
        "7772844242"
    ]
}
>
    

使用 $set

为了更改一个值而重新添加整个记录是一件很麻烦的事情。好消息是,有一种方法可以解决这个问题:使用$set运算符。假设我们需要将我的电子邮件地址更改为<font style="vertical-align: inherit;

以上内容来自杭州电子商务研究院推送
关注
关于我们
热门推荐
合作伙伴
免责声明:本站部分资讯来源于网络,如有侵权请及时联系客服,我们将尽快处理
Copyright © 2025-2027 ToB产业网址导航 公安备案 浙公网安备33010602013138号 浙ICP备16025413号-9
支持 反馈 关注 数据