MongoDB Insert the Documents into a Collection (Insert a single document)
kodingwindow@kw:~$ mongosh
...

DB> show dbs
DB        8.00 KiB
admin    40.00 KiB
config  108.00 KiB
local    40.00 KiB

DB> db.getCollectionNames()
[ "holders" ]

DB> db.holders.insertOne(
... {
... account_no: 2562348989,
... name: 'James Moore',
... city: 'Phoenix',
... DOB: new Date('1985-05-26'),
... bank: 'Barclays',
... amount: 5000,
... loan: [{
... Personal: 5660,
... Home: 15000
... }]
... })
{
  acknowledged: true,
  insertedId: ObjectId("6515552821cddd458cbbcc91")
}
Insert multiple documents
DB> db.holders.insertMany([
... {
... account_no: 2562348990,
... name: 'Donald Taylor',
... city: 'Irvine',
... DOB: new Date('1990-08-20'),
... bank: 'Citi',
... amount: 7000,
... loan: [{
... Home: 10000,
... Car: 20000
... }]
... },
... {
... account_no: 2562348991,
... name: 'Edward Parkar',
... city: 'Irvine',
... DOB: new Date('1994-01-29'),
... bank: 'ICICI',
... amount: 95000,
... loan: [{
... Personal: 25000,
... Home: 450000,
... Car: 10000
... }]
... },
... {
... account_no: 2562348992,
... name: 'Ryan Bakshi',
... city: 'Mumbai',
... DOB: new Date('1982-01-14'),
... bank: 'Citi',
... amount: 50000,
... loan: [{
... Personal: null,
... Home: null,
... Car: null
... }]
... }
... ])
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId("6515554d21cddd458cbbcc92"),
    '1': ObjectId("6515554d21cddd458cbbcc93"),
    '2': ObjectId("6515554d21cddd458cbbcc94")
  }
}
Show databases and collection info
DB> show databases
DB       40.00 KiB
admin    40.00 KiB
config  108.00 KiB
local    40.00 KiB

DB> db.holders.countDocuments()
4

DB> db.holders.estimatedDocumentCount()
4

DB> db.getCollectionInfos()
[
  {
    name: 'holders',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("d4b53141-e352-4cc5-8fd8-b7a37b36b205")
    },
    idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
  }
]
Advertisement