埋め込まれているMongoDBドキュメントのフィールドをインクリメントしますか?
たとえば、ここでは、StudentDetails内にあるMongoDBのStudentScoresをインクリメントしています-
... "StudentScores": {
... "StudentMathScore": 90,
... "StudentMongoDBScore": 78
... } まず、ドキュメントを使用してコレクションを作成しましょう-
> db.embeddedValueIncrementDemo.insertOne(
... {
... "StudentDetails": {
... "StudentScores": {
... "StudentMathScore": 90,
... "StudentMongoDBScore": 78
... }
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2b670345990cee87fd896")
} 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.embeddedValueIncrementDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cd2b670345990cee87fd896"),
"StudentDetails" : {
"StudentScores" : {
"StudentMathScore" : 90,
"StudentMongoDBScore" : 78
}
}
} 以下は、エンベディッドバリューをインクリメントするためのクエリです。ここでは、StudentMongoDBScoreをインクリメントしています-
> db.embeddedValueIncrementDemo.update({ _id: new ObjectId("5cd2b670345990cee87fd896") }, { $inc: { "StudentDetails.StudentScores.StudentMongoDBScore": 20 } }, { upsert: true, safe: true }, null);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) すべてのドキュメントをもう一度確認しましょう-
> db.embeddedValueIncrementDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cd2b670345990cee87fd896"),
"StudentDetails" : {
"StudentScores" : {
"StudentMathScore" : 90,
"StudentMongoDBScore" : 98
}
}
} -
idがドキュメントフィールドの配列値と等しい場合に除外するMongoDBクエリ
このために、$inと一緒に$notを使用します。ドキュメントを使用してコレクションを作成しましょう- [ { id: "101", subjectid: [ "102" ] }, { id: "102", &nb
-
MongoDBを使用して埋め込みドキュメントの配列でクエリをフィルタリングしますか?
これには、MongoDBでaggregate()を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo736.insertOne( ... { ... "_id": "101", ... "details1": [ ... { ... &q