IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> Cp3 Creating and Manipulating Documents -> 正文阅读

[大数据]Cp3 Creating and Manipulating Documents

Inserting New Documents

?

Learn Data Modeling

?

?How does the value fo _id get assigned to a document.

it is automatically generated as an ObjectId type value.

You can select a non ObjectId type value when inserting a new document, as long as that value is unique to this collection.

Inserting New Documents - insert() and errors

In this lesson we used the following commands:

mongoimport --uri="mongodb+srv://<username>:<password>@<cluster>.mongodb.net/sample_supplies" sales.json

mongoimport --uri="mongodb+srv://m001-student:m001-mongodb-basics@Sandbox.mongodb.net/sample_supplies" sales.json

Step one : Connect to the Atlas cluster

mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin"

Step two: navigate to the database that we need:

use sample_training

Step three, get a random document from the collection:

db.inspections.findOne();

Step four, copy this random document, and try to insert in into the collection:

db.inspections.insert({ "_id" : ObjectId("56d61033a378eccde8a8354f"), "id" : "10021-2015-ENFO", "certificate_number" : 9278806, "business_name" : "ATLIXCO DELI GROCERY INC.", "date" : "Feb 20 2015", "result" : "No Violation Issued", "sector" : "Cigarette Retail Dealer - 127", "address" : { "city" : "RIDGEWOOD", "zip" : 11385, "street" : "MENAHAN ST", "number" : 1712 } })

db.inspections.insert({ "id" : "10021-2015-ENFO", "certificate_number" : 9278806, "business_name" : "ATLIXCO DELI GROCERY INC.", "date" : "Feb 20 2015", "result" : "No Violation Issued", "sector" : "Cigarette Retail Dealer - 127", "address" : { "city" : "RIDGEWOOD", "zip" : 11385, "street" : "MENAHAN ST", "number" : 1712 } }) db.inspections.find({"id" : "10021-2015-ENFO", "certificate_number" : 9278806}).pretty()

-------------------------------------------------------------------------------------------------

ep:?

C:\Users\psmax>mongosh "mongodb+srv://sandbox.gnors.mongodb.net/myFirstDatabase" --apiVersion 1 --username m001-student
Enter password: *******************
Current Mongosh Log ID: 6242741bf6d6a70ca0c97b21
Connecting to: ? ? ? ? ?mongodb+srv://sandbox.gnors.mongodb.net/myFirstDatabase?appName=mongosh+1.3.1
Using MongoDB: ? ? ? ? ?5.0.6 (API Version 1)
Using Mongosh: ? ? ? ? ?1.3.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Warning: Found ~/.mongorc.js, but not ~/.mongoshrc.js. ~/.mongorc.js will not be loaded.
? You may want to copy or rename ~/.mongorc.js to ~/.mongoshrc.js.
Atlas atlas-nxi8gd-shard-0 [primary] myFirstDatabase> show dbs
sample_airbnb ? ? ? 56.4 MB
sample_analytics ? ?9.58 MB
sample_geospatial ? 1.46 MB
sample_guides ? ? ? ? 41 kB
sample_mflix ? ? ? ?50.1 MB
sample_restaurants ?6.97 MB
sample_supplies ? ? 1.18 MB
sample_training ? ? 58.2 MB
sample_weatherdata ? 2.9 MB
admin ? ? ? ? ? ? ? ?385 kB
local ? ? ? ? ? ? ? 8.65 GB
Atlas atlas-nxi8gd-shard-0 [primary] myFirstDatabase> use sample_
use sample_airbnb ? ? ? use sample_analytics ? ?use sample_geospatial ? use sample_guides ? ? ? use sample_mflix ? ? ? ?use sample_restaurants ?use sample_supplies ? ? use sample_training
use sample_weatherdata

Atlas atlas-nxi8gd-shard-0 [primary] myFirstDatabase> use sample_training
switched to db sample_training
Atlas atlas-nxi8gd-shard-0 [primary] sample_training>

Atlas atlas-nxi8gd-shard-0 [primary] sample_training> db.inspections.findOne()
{
? _id: ObjectId("56d61033a378eccde8a8354f"),
? id: '10021-2015-ENFO',
? certificate_number: 9278806,
? business_name: 'ATLIXCO DELI GROCERY INC.',
? date: 'Feb 20 2015',
? result: 'No Violation Issued',
? sector: 'Cigarette Retail Dealer - 127',
? address: { city: 'RIDGEWOOD', zip: 11385, street: 'MENAHAN ST', number: 1712 }
}
Atlas atlas-nxi8gd-shard-0 [primary] sample_training>

db.inspections.insert({ "_id" : ObjectId("56d61033a378eccde8a8354f"), "id" : "10021-2015-ENFO", "certificate_number" : 9278806, "business_name" : "ATLIXCO DELI GROCERY INC.", "date" : "Feb 20 2015", "result" : "No Violation Issued", "sector" : "Cigarette Retail Dealer - 127", "address" : { "city" : "RIDGEWOOD", "zip" : 11385, "street" : "MENAHAN ST", "number" : 1712 } })

db.inspections.insert({ "id" : "10021-2015-ENFO", "certificate_number" : 9278806, "business_name" : "ATLIXCO DELI GROCERY INC.", "date" : "Feb 20 2015", "result" : "No Violation Issued", "sector" : "Cigarette Retail Dealer - 127", "address" : { "city" : "RIDGEWOOD", "zip" : 11385, "street" : "MENAHAN ST", "number" : 1712 } }) db.inspections.find({"id" : "10021-2015-ENFO", "certificate_number" : 9278806}).pretty()

?

?

Select all true statements from the following list:

MongoDB can store duplicate documents in the same collection,as long as their _id values are different.

If a document is inserted without a provided _id value,then the _id field and value will be automatically generated for the inserted document before insertion.

Inserting New Documents - insert() order

Insert three test documents:

db.inspections.insert([ { "test": 1 }, { "test": 2 }, { "test": 3 } ])

db.inspections.insert([{ "_id": 1, "test": 1 },{ "_id": 1, "test": 2 }, { "_id": 3, "test": 3 }])

Find the documents with?_id: 1

db.inspections.find({ "_id": 1 })

Insert multiple documents specifying the?_id?values, and using the?"ordered": false?option.

db.inspections.insert([{ "_id": 1, "test": 1 },{ "_id": 1, "test": 2 }, { "_id": 3, "test": 3 }],{ "ordered": false })

Insert multiple documents with?_id: 1?with the default?"ordered": true?setting

db.inspection.insert([{ "_id": 1, "test": 1 },{ "_id": 3, "test": 3 }])

View?collections?in the active?db

show collections

Switch the active?db?to?training

use training

View all available databases

show dbs

Atlas atlas-nxi8gd-shard-0 [primary] training> db.pets.insert([{ "pet": "cat" }, { "pet": "dog" }, { "pet": "fish" }])
{
? acknowledged: true,
? insertedIds: {
? ? '0': ObjectId("6242c3abb17c23387790a625"),
? ? '1': ObjectId("6242c3abb17c23387790a626"),
? ? '2': ObjectId("6242c3abb17c23387790a627")
? }
}
Atlas atlas-nxi8gd-shard-0 [primary] training> db.pets.insert([{ "_id": 1, "pet": "cat" },
... ? ? ? ? ? ? ? ? { "_id": 1, "pet": "dog" },
... ? ? ? ? ? ? ? ? { "_id": 3, "pet": "fish" },
... ? ? ? ? ? ? ? ? { "_id": 4, "pet": "snake" }], { "ordered": true })

Uncaught:
MongoBulkWriteError: E11000 duplicate key error collection: training.pets index: _id_ dup key: { _id: 1 }
Result: BulkWriteResult {
? result: {
? ? ok: 1,
? ? writeErrors: [
? ? ? WriteError {
? ? ? ? err: {
? ? ? ? ? index: 1,
? ? ? ? ? code: 11000,
? ? ? ? ? errmsg: 'E11000 duplicate key error collection: training.pets index: _id_ dup key: { _id: 1 }',
? ? ? ? ? errInfo: undefined,
? ? ? ? ? op: { _id: 1, pet: 'dog' }
? ? ? ? }
? ? ? }
? ? ],
? ? writeConcernErrors: [],
? ? insertedIds: [
? ? ? { index: 0, _id: 1 },
? ? ? { index: 1, _id: 1 },
? ? ? { index: 2, _id: 3 },
? ? ? { index: 3, _id: 4 }
? ? ],
? ? nInserted: 1,
? ? nUpserted: 0,
? ? nMatched: 0,
? ? nModified: 0,
? ? nRemoved: 0,
? ? upserted: [],
? ? opTime: { ts: Timestamp({ t: 1648542650, i: 9 }), t: Long("121") }
? }
}
Atlas atlas-nxi8gd-shard-0 [primary] training>

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-04-01 00:08:40  更:2022-04-01 00:12:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 4:21:59-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码