在 MongoDB 中查詢多個引數?
要在 MongoDB 中查詢多個引數,您可以使用點(.)符號。我們先使用文件建立集合 -
> db.multipleParametersDemo.insertOne( ... { ... "CustomerName" : "Larry", ... "CustomerDetails" : [ ... { ... "CustomerCountryName" : "US", ... "CustomerBankName" : "HDFC", ... "CustomerBalance" : 17363, ... } ... ], ... "Purchase" : 1456, ... ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd10f9ce3526dbddbbfb60a") }
以下是使用 find() 方法顯示集合中所有文件的查詢 -
> db.multipleParametersDemo.find().pretty();
這將產生以下輸出 -
{ "_id" : ObjectId("5cd10f9ce3526dbddbbfb60a"), "CustomerName" : "Larry", "CustomerDetails" : [ { "CustomerCountryName" : "US", "CustomerBankName" : "HDFC", "CustomerBalance" : 17363 } ], "Purchase" : 1456 }
以下是您可以在 MongoDB 中查詢多個引數的方式 -
> db.multipleParametersDemo.find({CustomerName: 'Larry', 'CustomerDetails.CustomerCountryName': 'US'}).count();
這將產生以下輸出 -
1
廣告