Skip to content

MongoDb

Thomas Czogalik edited this page Mar 2, 2021 · 7 revisions

Queries

find

  • find({field:value}) // finde Objekt mit field === value)
  • find({field: {$exists: true/false} }) // finde Objekt, dass Feld (nicht) hat
  • pattern:
    • find({value: /pattern/})
  • { $and: [ { expression1 }, { expression2 } , ... , { expressionN } ] }
  • update({field:value}, {$set:{somefield:someValue}})
  • update({field:value}, {$set:{somefield:someValue}}, false, true) // multiupdate
db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        // Available starting in MongoDB 4.2
   }
)

Field comparsion

({$where:"this.fieldA == this.fieldB"})
({$where:"this.fieldA != this.fieldB"})
({$where:"this.fieldA >= this.fieldB"})

DateRange

{ 
  someDateField: {
    $gte:IsoDate(y-m-dTH:M:S),
    $lt:IsoDate(y-m-dTH:M:S)
  }
}

Indexing

  • Equality tests, in any order
  • Sort fields, in the order requested by the query
  • Range filters, in increasing order of cardinality

Java

Testing

Clone this wiki locally