MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

Mögliche Abfrage mit MongoDB

Verwenden Sie $and :

db.getCollection('collection_name').find({
$and: [{
  TitleData: {
    $elemMatch: {
      UserId: ObjectId("57a87f5cc48933119cb96f9b"),
      UserId: ObjectId("57a87f5cc48933119cb96fa7")
    }
  }
}, {
  TitleData: {
    $elemMatch: {
      $ne: {
        "Res": 2
      }
    }
  }
}]
}));

Testen Sie die Ausgabe von der Konsole mit Ihren Sammlungsobjekten:

> db.collection.find({ $and: [{     TitleData: {       $elemMatch: {         UserId: ObjectId("57a87f5cc48933119cb96f9b"),          UserId: ObjectId("57a87f5cc48933119cb96fa7")       }     }   },{ TitleData: { $elemMatch: { $ne: { "Res": 2 } } }}]});
{ "_id" : ObjectId("57a8a6c3c48933256cfd8368"), "Title" : "T1", "TitleData" : [     {   "UserId" : ObjectId("57a87f5cc48933119cb96f9b"),    "Res" : 2 },    {   "UserId" : ObjectId("57a87f5cc48933119cb96fa7"),    "Res" : 2 },    {   "UserId" : ObjectId("57a87f5cc48933119cb96f96"),    "Res" : 2 },    {   "UserId" : ObjectId("57a87f5cc48933119cb96f9c"),    "Res" : 2 },    {   "UserId" : ObjectId("57a87f5cc48933119cb96f9d"),    "Res" : 0 } ] }

BEARBEITEN Nicht alle Konsolenausgaben hinzugefügt...