MongoDB Projection (limiting and sorting data) - Disable ObjectId
kodingwindow@kw:~$ mongosh
...

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

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

DB> db.holders.find({},{name:1,city:1,_id:0})
[
  { name: 'James Moore', city: 'Phoenix' },
  { name: 'Donald Taylor', city: 'Irvine' },
  { name: 'Edward Parkar', city: 'Irvine' },
  { name: 'Ryan Bakshi', city: 'Mumbai' }
]
MongoDB Projection (limiting and sorting data) - limit() and skip() methods
DB> db.holders.find({},{account_no: 2562348989,city: 'Irvine'}).limit(2)
[
  {
    _id: ObjectId("6515552821cddd458cbbcc91"),
    account_no: 2562348989,
    city: 'Irvine'
  },
  {
    _id: ObjectId("6515554d21cddd458cbbcc92"),
    account_no: 2562348990,
    city: 'Irvine'
  }
]

DB> db.holders.find({},{account_no: 2562348989,city: 'Irvine'}).limit(2).skip(1)
[
  {
    _id: ObjectId("6515554d21cddd458cbbcc92"),
    account_no: 2562348990,
    city: 'Irvine'
  },
  {
    _id: ObjectId("6515554d21cddd458cbbcc93"),
    account_no: 2562348991,
    city: 'Irvine'
  }
]
City names are in ascending order
DB> db.holders.find().sort({city: 1})
[
  {
    _id: ObjectId("6515554d21cddd458cbbcc92"),
    account_no: 2562348990,
    name: 'Donald Taylor',
    city: 'Irvine',
    DOB: ISODate("1990-08-20T00:00:00.000Z"),
    bank: 'Citi',
    amount: 7000,
    loan: [ { Home: 10000, Car: 20000 } ]
  },
  {
    _id: ObjectId("6515554d21cddd458cbbcc93"),
    account_no: 2562348991,
    name: 'Edward Parkar',
    city: 'Irvine',
    DOB: ISODate("1994-01-29T00:00:00.000Z"),
    bank: 'ICICI',
    amount: 95000,
    loan: [ { Personal: 25000, Home: 450000, Car: 10000 } ]
  },
  {
    _id: ObjectId("6515554d21cddd458cbbcc94"),
    account_no: 2562348992,
    name: 'Ryan Bakshi',
    city: 'Mumbai',
    DOB: ISODate("1982-01-14T00:00:00.000Z"),
    bank: 'Citi',
    amount: 50000,
    loan: [ { Personal: null, Home: null, Car: null } ]
  },
  {
    _id: ObjectId("6515552821cddd458cbbcc91"),
    account_no: 2562348989,
    name: 'James Moore',
    city: 'Phoenix',
    DOB: ISODate("1985-05-26T00:00:00.000Z"),
    bank: 'Barclays',
    amount: 5000,
    loan: [ { Personal: 5660, Home: 15000 } ]
  }
]
Account numbers are in descending order
DB> db.holders.find().sort({account_no: -1})
[
  {
    _id: ObjectId("6515554d21cddd458cbbcc94"),
    account_no: 2562348992,
    name: 'Ryan Bakshi',
    city: 'Mumbai',
    DOB: ISODate("1982-01-14T00:00:00.000Z"),
    bank: 'Citi',
    amount: 50000,
    loan: [ { Personal: null, Home: null, Car: null } ]
  },
  {
    _id: ObjectId("6515554d21cddd458cbbcc93"),
    account_no: 2562348991,
    name: 'Edward Parkar',
    city: 'Irvine',
    DOB: ISODate("1994-01-29T00:00:00.000Z"),
    bank: 'ICICI',
    amount: 95000,
    loan: [ { Personal: 25000, Home: 450000, Car: 10000 } ]
  },
  {
    _id: ObjectId("6515554d21cddd458cbbcc92"),
    account_no: 2562348990,
    name: 'Donald Taylor',
    city: 'Irvine',
    DOB: ISODate("1990-08-20T00:00:00.000Z"),
    bank: 'Citi',
    amount: 7000,
    loan: [ { Home: 10000, Car: 20000 } ]
  },
  {
    _id: ObjectId("6515552821cddd458cbbcc91"),
    account_no: 2562348989,
    name: 'James Moore',
    city: 'Phoenix',
    DOB: ISODate("1985-05-26T00:00:00.000Z"),
    bank: 'Barclays',
    amount: 5000,
    loan: [ { Personal: 5660, Home: 15000 } ]
  }
]
Advertisement