Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.3 kB
4
Indexable
Never
Query: db.products.find({}, {"name": 1, "price": 1, _id: 0})
Result: [
  { name: 'AC7 Phone', price: 320 },
  { name: 'Phone Service Core Plan' },
  { name: 'AC3 Phone', price: 200 },
  { name: 'Phone Service Basic Plan' },
  { name: 'Phone Extended Warranty', price: 38 },
  { name: 'Phone Service Family Plan' },
  { name: 'AC3 Case Red', price: 12 },
  { name: 'AC3 Series Charger', price: 19 },
  { name: 'AC3 Case Black', price: 12.5 },
  { name: 'AC3 Case Green', price: 12 },
  { name: 'Cable TV Basic Service Package' }
]

Query: db.products.find({"type": "accessory"}, {"name": 1, "price": 1, _id: 0})
Result: [
  { name: 'AC3 Case Red', price: 12 },
  { name: 'AC3 Series Charger', price: 19 },
  { name: 'AC3 Case Black', price: 12.5 },
  { name: 'AC3 Case Green', price: 12 }
]

Query: db.products.find({"price": {"$gte": 12, "$lte": 20}}, {"name": 1, "price": 1, _id: 0})
Result: [
  { name: 'AC3 Case Red', price: 12 },
  { name: 'AC3 Series Charger', price: 19 },
  { name: 'AC3 Case Black', price: 12.5 },
  { name: 'AC3 Case Green', price: 12 }
]

Query: db.products.find({"type": {"$ne": "accessory"}}, {"name": 1, "price": 1, _id: 1})
Result: [
  { _id: 'ac7', name: 'AC7 Phone', price: 320 },
  {
    _id: ObjectId('507d95d5719dbef170f15bff'),
    name: 'Phone Service Core Plan'
  },
  { _id: 'ac3', name: 'AC3 Phone', price: 200 },
  {
    _id: ObjectId('507d95d5719dbef170f15bfe'),
    name: 'Phone Service Basic Plan'
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bfb'),
    name: 'Phone Extended Warranty',
    price: 38
  },
  {
    _id: ObjectId('507d95d5719dbef170f15c00'),
    name: 'Phone Service Family Plan'
  },
  {
    _id: ObjectId('507d95d5719dbef170f15c01'),
    name: 'Cable TV Basic Service Package'
  }
]

Query: db.products.find({"type": {"$in": ["accessory", "service"]}}, {"name": 1, "price": 1, _id: 1, "type": 1})
Result: [
  {
    _id: ObjectId('507d95d5719dbef170f15bff'),
    name: 'Phone Service Core Plan',
    type: 'service'
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bfe'),
    name: 'Phone Service Basic Plan',
    type: 'service'
  },
  {
    _id: ObjectId('507d95d5719dbef170f15c00'),
    name: 'Phone Service Family Plan',
    type: 'service'
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bfd'),
    name: 'AC3 Case Red',
    type: [ 'accessory', 'case' ],
    price: 12
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bf9'),
    name: 'AC3 Series Charger',
    type: [ 'accessory', 'charger' ],
    price: 19
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bfc'),
    name: 'AC3 Case Black',
    type: [ 'accessory', 'case' ],
    price: 12.5
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bfa'),
    name: 'AC3 Case Green',
    type: [ 'accessory', 'case' ],
    price: 12
  }
]

Query: db.products.find({ "type": { "$exists": true } }, { "name": 1, "price": 1, _id: 1, "type": 1 })
Result: [
  { _id: 'ac7', name: 'AC7 Phone', type: 'phone', price: 320 },
  {
    _id: ObjectId('507d95d5719dbef170f15bff'),
    name: 'Phone Service Core Plan',
    type: 'service'
  },
  { _id: 'ac3', name: 'AC3 Phone', type: 'phone', price: 200 },
  {
    _id: ObjectId('507d95d5719dbef170f15bfe'),
    name: 'Phone Service Basic Plan',
    type: 'service'
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bfb'),
    name: 'Phone Extended Warranty',
    type: 'warranty',
    price: 38
  },
  {
    _id: ObjectId('507d95d5719dbef170f15c00'),
    name: 'Phone Service Family Plan',
    type: 'service'
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bfd'),
    name: 'AC3 Case Red',
    type: [ 'accessory', 'case' ],
    price: 12
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bf9'),
    name: 'AC3 Series Charger',
    type: [ 'accessory', 'charger' ],
    price: 19
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bfc'),
    name: 'AC3 Case Black',
    type: [ 'accessory', 'case' ],
    price: 12.5
  },
  {
    _id: ObjectId('507d95d5719dbef170f15bfa'),
    name: 'AC3 Case Green',
    type: [ 'accessory', 'case' ],
    price: 12
  },
  {
    _id: ObjectId('507d95d5719dbef170f15c01'),
    name: 'Cable TV Basic Service Package',
    type: 'tv'
  }
]

Query: db.products.find({ "type": { "$all": ["accessory", "service"] } }, { "name": 1, "price": 1, _id: 1, "type": 1 })
Result: 
Leave a Comment