MongoDBにフィールドが存在する場合にのみ条件を適用するにはどうすればよいですか?
これには$または演算子を使用できます。まず、ドキュメントを使用してコレクションを作成しましょう-
> db.applyConditionDemo.insertOne({"StudentName":"Larry","StudentAge":21,"StudentMarks":45});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cb80b78623186894665ae36")
}
> db.applyConditionDemo.insertOne({"StudentName":"Sam","StudentAge":23,"StudentMarks":55});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cb80b87623186894665ae37")
}
> db.applyConditionDemo.insertOne({"StudentName":"David","StudentAge":21,"StudentMarks":65});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cb80b95623186894665ae38")
}
> db.applyConditionDemo.insertOne({"StudentName":"Carol","StudentAge":24,"StudentMarks":78});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cb80ba3623186894665ae39")
}
> db.applyConditionDemo.insertOne({"StudentName":"Chris","StudentAge":21,"StudentMarks":88});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cb80bae623186894665ae3a")
}
> db.applyConditionDemo.insertOne({"StudentName":"Robert","StudentMarks":98});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cb80c3d623186894665ae3b")
} 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.applyConditionDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cb80b78623186894665ae36"),
"StudentName" : "Larry",
"StudentAge" : 21,
"StudentMarks" : 45
}
{
"_id" : ObjectId("5cb80b87623186894665ae37"),
"StudentName" : "Sam",
"StudentAge" : 23,
"StudentMarks" : 55
}
{
"_id" : ObjectId("5cb80b95623186894665ae38"),
"StudentName" : "David",
"StudentAge" : 21,
"StudentMarks" : 65
}
{
"_id" : ObjectId("5cb80ba3623186894665ae39"),
"StudentName" : "Carol",
"StudentAge" : 24,
"StudentMarks" : 78
}
{
"_id" : ObjectId("5cb80bae623186894665ae3a"),
"StudentName" : "Chris",
"StudentAge" : 21,
"StudentMarks" : 88
}
{
"_id" : ObjectId("5cb80c3d623186894665ae3b"),
"StudentName" : "Robert",
"StudentMarks" : 98
} 以下は、フィールドが存在する場合にのみ条件を適用するためのクエリです-
> db.applyConditionDemo.find({ $or: [ { StudentAge: { $exists:false } }, { StudentAge:21 } ]}).pretty(); これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cb80b78623186894665ae36"),
"StudentName" : "Larry",
"StudentAge" : 21,
"StudentMarks" : 45
}
{
"_id" : ObjectId("5cb80b95623186894665ae38"),
"StudentName" : "David",
"StudentAge" : 21,
"StudentMarks" : 65
}
{
"_id" : ObjectId("5cb80bae623186894665ae3a"),
"StudentName" : "Chris",
"StudentAge" : 21,
"StudentMarks" : 88
}
{
"_id" : ObjectId("5cb80c3d623186894665ae3b"),
"StudentName" : "Robert",
"StudentMarks" : 98
} -
MongoDBクエリを実行して、特定のフィールド値のみを更新するにはどうすればよいですか?
例を見て、ドキュメントを使用してコレクションを作成しましょう- > db.demo557.insertOne({Name:"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f28e954b4472ed3e8e864") } > db.demo557.insertOne({Name:"David"});{ "acknowledged" :
-
MongoDB 4でドキュメントを並べ替えて、単一のフィールドのみを表示するにはどうすればよいですか?
MongoDB 4でドキュメントを並べ替えるには、sort()を使用します。並べ替えられた単一のフィールドのみを表示するには、1に設定します。 ドキュメントを使用してコレクションを作成しましょう- > db.demo611.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987110f6b89257f5584d83") } > db.demo61