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>