MongoDB
 Computer >> コンピューター >  >> プログラミング >> MongoDB

すべてのMongoDBドキュメントを取得しますが、2つの基準を持つドキュメントは取得しませんか?


以下のいずれかのケースに従って、特定の基準を持つすべてのMongoDBドキュメントを取得するには

ケース1 以下は、$ne演算子を使用して単一の基準なしですべてのドキュメントを取得するためのクエリです

db.yourCollectionName.find({yourFieldName:{$ne:"yourValue"}}).pretty();

ケース2 以下は、$nin演算子を使用して2つの特定の基準なしですべてのドキュメントを取得するためのクエリです

db.yourCollectionName.find({yourFieldName:{$nin:["yourValue1","yourValue2"]}}).pretty();

まずコレクションを作成しましょう。以下は、ドキュメントを含むコレクションを作成するためのクエリです

>db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Larry","StudentSubjectName":"Java"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c993d82330fd0aa0d2fe4d2")
}
>db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Chris","StudentSubjectName":"C++"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c993d8f330fd0aa0d2fe4d3")
}
>db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Robert","StudentSubjectName":"C"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c993d99330fd0aa0d2fe4d4")
}
>db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"David","StudentSubjectName":"Python"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c993da4330fd0aa0d2fe4d5")
}

以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです

> db.findAllExceptFromOneOrtwoDemo.find().pretty();

これにより、次の出力が生成されます

{
   "_id" : ObjectId("5c993d82330fd0aa0d2fe4d2"),
   "StudentName" : "Larry",
   "StudentSubjectName" : "Java"
}
{
   "_id" : ObjectId("5c993d8f330fd0aa0d2fe4d3"),
   "StudentName" : "Chris",
   "StudentSubjectName" : "C++"
}
{
   "_id" : ObjectId("5c993d99330fd0aa0d2fe4d4"),
   "StudentName" : "Robert",
   "StudentSubjectName" : "C"
}
{
   "_id" : ObjectId("5c993da4330fd0aa0d2fe4d5"),
   "StudentName" : "David",
   "StudentSubjectName" : "Python"
}

ケース1 単一の基準

以下はクエリです

> db.findAllExceptFromOneOrtwoDemo.find({StudentSubjectName:{$ne:"C"}}).pretty();

これにより、次の出力が生成されます

{
   "_id" : ObjectId("5c993d82330fd0aa0d2fe4d2"),
   "StudentName" : "Larry",
   "StudentSubjectName" : "Java"
}
{
   "_id" : ObjectId("5c993d8f330fd0aa0d2fe4d3"),
   "StudentName" : "Chris",
   "StudentSubjectName" : "C++"
}
{
   "_id" : ObjectId("5c993da4330fd0aa0d2fe4d5"),
   "StudentName" : "David",
   "StudentSubjectName" : "Python"
}

ケース2 2つの基準

以下はクエリです

>db.findAllExceptFromOneOrtwoDemo.find({StudentSubjectName:{$nin:["C++","Python"]}}).pretty();

これにより、次の出力が生成されます

{
   "_id" : ObjectId("5c993d82330fd0aa0d2fe4d2"),
   "StudentName" : "Larry",
   "StudentSubjectName" : "Java"
}
{
   "_id" : ObjectId("5c993d99330fd0aa0d2fe4d4"),
   "StudentName" : "Robert",
   "StudentSubjectName" : "C"
}

  1. マークが最も少ない2つのドキュメントを取得するためのMongoDB集計

    ソートされたマークのリストを取得するには、$sortを使用します。 $ limit:2を使用します マークが最も少ないそのようなドキュメントを2つだけ表示します。ドキュメントを使用してコレクションを作成しましょう- > db.demo709.insertOne({Name:"John","Marks":75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839005d33e20ed

  2. Aggregationを使用して、MongoDBのすべての値を照合します

    MongoDBのすべての値を一致させるには、Aggregationで$andとともに$matchを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo574.insertOne( ...    { ...       "details1": { ...          "details2": { ...             "dueDate&q