MongoDB for Developers with Python Transcripts
Chapter: MongoDB's shell and native query syntax
Lecture: Inserting documents in MongoDB

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Inserts are one of the simpler operations in MongoDB actually.
0:04 So we just go db.collection name, in this case db.book.insert
0:08 and we give it the thing to insert.
0:10 Now, if we don't specify an id, an _id
0:13 which generally we want to let the database generate
0:16 but it's not always true, like we could have people and their primary key,
0:20 their id could be there social security number, and that you would provide,
0:23 so in this case, we're not going to provide an id,
0:26 we're going to type in title and isbn, and those kinds of things.
0:29 And then if we just do a find, that would come back and get the first one
0:32 maybe say this is our first insert, we'd get something back like this,
0:35 let's say we specified the isbn, the title,
0:37 the author, the published and the publisher,
0:40 this is a relationship over to the publisher table,
0:42 which we haven't played with yet.
0:44 So those were all set by us, you can see "Winning With MongoDB"
0:47 and down here we have "Winning With MongoDB:,
0:50 but the _id, because we didn't specify it was auto generated to an object id.
0:54 So unless you have a good reason to pick another type of id,
0:57 this is probably the best one for Mongo, but it could have been a string,
1:00 like I said, it could have been a social security number
1:03 or it could be just numerical if you want to have a 1234,
1:06 all of those kind of put the burden on you to manage the uniqueness of that id
1:10 and there is a unique disconstraint on _id for every table or collection.
1:15 So that's how inserts work, you just give it this document
1:17 and it stores it more or less directly in the database
1:20 except for that it will generate this _id as an object id if needed.