Find documents by field value
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({city: 'Phoenix'})
[
  {
    _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 } ]
  }
]

DB> db.holders.find({account_no: 2562348989})
[
  {
    _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 } ]
  }
]
Find documents using $and operator
DB> db.holders.find(
... {$and: [{city: 'Irvine'},{account_no: 2562348991}]}
... )
[
  {
    _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 } ]
  }
]
Find documents using $or operator
DB> db.holders.find(
... {$or: [{city: 'Irvine'},{account_no: 2562348991}]}
... )
[
  {
    _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 } ]
  }
]
Find documents using $lte operator
DB> db.holders.find(
... {account_no:{$lte: 2562348990}}
... )
[
  {
    _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 } ]
  },
  {
    _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 } ]
  }
]
Find documents using $not operator
DB> db.holders.find(
... {account_no:{$not:{$lte: 2562348990}}}
... )
[
  {
    _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 } ]
  }
]
Find documents using $nor operator
DB> db.holders.find( 
... {$nor:[{account_no: 2562348989},{account_no: 2562348991}]}
... )
[
  {
    _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("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 } ]
  }
]
Advertisement