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. So we just go db.collection name, in this case db.book.insert
0:09 and we give it the thing to insert. Now, if we don't specify an id, an _id which generally we want to let the database generate
0:17 but it's not always true, like we could have people and their primary key, their id could be there social security number, and that you would provide,
0:24 so in this case, we're not going to provide an id, we're going to type in title and isbn, and those kinds of things.
0:30 And then if we just do a find, that would come back and get the first one maybe say this is our first insert, we'd get something back like this,
0:36 let's say we specified the isbn, the title, the author, the published and the publisher, this is a relationship over to the publisher table,
0:43 which we haven't played with yet. So those were all set by us, you can see "Winning With MongoDB" and down here we have "Winning With MongoDB:,
0:51 but the _id, because we didn't specify it was auto generated to an object id. So unless you have a good reason to pick another type of id,
0:58 this is probably the best one for Mongo, but it could have been a string, like I said, it could have been a social security number
1:04 or it could be just numerical if you want to have a 1234, all of those kind of put the burden on you to manage the uniqueness of that id
1:11 and there is a unique disconstraint on _id for every table or collection. So that's how inserts work, you just give it this document
1:18 and it stores it more or less directly in the database except for that it will generate this _id as an object id if needed.


Talk Python's Mastodon Michael Kennedy's Mastodon