找到 1349 篇文章 關於 MongoDB

將MongoDB shell中的prettyprint設定為預設值?

Nishtha Thakur
更新於 2019年7月30日 22:30:25

168 次檢視

您可以對遊標物件呼叫 pretty() 函式,以便在 MongoDB shell 中進行 prettyprint。語法如下:db.yourCollectionName.find().pretty();為了理解這個概念,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下:>db.prettyDemo.insertOne({"ClientName":"Larry", "ClientAge":27, "ClientFavoriteCountry":["US", "UK"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a440de01f572ca0ccf5f2") } >db.prettyDemo.insertOne({"ClientName":"Mike", "ClientAge":57, "ClientFavoriteCountry":["AUS", "UK"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a4420e01f572ca0ccf5f3") }使用 find() 方法顯示集合中的所有文件。查詢如下:> db.prettyDemo.find();輸出如下:{ "_id" : ObjectId("5c8a440de01f572ca0ccf5f2"), "ClientName" : "Larry", "ClientAge" ... 閱讀更多

如何在 Mongo shell 中列出所有集合?

Anvi Jain
更新於 2019年7月30日 22:30:25

7K+ 次檢視

要在 Mongo shell 中列出所有集合,可以使用 getCollectionNames() 函式。語法如下:db.getCollectionNames();您可以使用另一個命令 collections。語法如下:show collections;要在 Mongo 中列出所有集合,可以使用以上兩個函式。查詢如下:> db.getCollectionNames();輸出如下:[    "ConvertStringToDateDemo",    "IdUpdateDemo",    "ProductsInformation",    "addFieldDemo",    "addNewFieldToEveryDocument",    "arrayInnerElementsDemo",    "arrayLengthGreaterThanOne",    "arrayOfArraysDemo",    "caseInsensitiveDemo",    "changeDataType",    "changeType",    "charactersAllowedDemo",    "charactersDemo",    "checkFieldContainsStringDemo",    "checkSequenceDemo",    "combinationOfArrayDemo",    "conditionalSumDemo",    "convertStringToNumberDemo",    "copyThisCollectionToSampleDatabaseDemo",    "countDemo",    "createSequenceDemo",    "distinctCountValuesDemo",    "distinctRecordDemo", ... 閱讀更多

如何從命令列刪除 MongoDB 資料庫?

Smita Kapse
更新於 2019年7月30日 22:30:25

257 次檢視

要從命令列刪除 MongoDB 資料庫,請使用以下語法:mongo yourDatabaseName --eval "db.dropDatabase()"為了理解上述語法,讓我們顯示 MongoDB 中的所有資料庫。查詢如下:> show dbs;輸出如下:StudentTracker 0.000GB admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB test 0.003GB刪除名為“StudentTracker”的資料庫。以下是從命令列刪除資料庫的查詢:C:\Program Files\MongoDB\Server\4.0\bin>mongo StudentTracker --eval "db.dropDatabase()"輸出如下:MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/StudentTracker?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("afc34e93-b4c0-46f0-85bd-b80ed17b8c11") } MongoDB server version: 4.0.5 { "dropped" ... 閱讀更多

如何在 MongoDB 中計算每個欄位/鍵的不同值的個數?

Smita Kapse
更新於 2019年7月30日 22:30:25

430 次檢視

您可以為此使用 distinct 命令。為了理解這個概念,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下:> db.distinctCountValuesDemo.insertOne({"StudentFirstName":"John", "StudentFavouriteSubject":["C", "C++", "Java", "MySQL", "C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a39f193b406bd3df60e07") } > db.distinctCountValuesDemo.insertOne({"StudentFirstName":"Larry", "StudentFavouriteSubject":["MongoDB", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a3a1193b406bd3df60e08") }使用 find() 方法顯示集合中的所有文件。查詢如下:> db.distinctCountValuesDemo.find().pretty();輸出如下:{    "_id" : ObjectId("5c8a39f193b406bd3df60e07"),    "StudentFirstName" : "John",    "StudentFavouriteSubject" : [ ... 閱讀更多

如何在 MongoDB 中查詢重複記錄?

Nishtha Thakur
更新於 2019年7月30日 22:30:25

2K+ 次檢視

您可以使用聚合框架在 MongoDB 中查詢重複記錄。為了理解這個概念,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下:> db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330293b406bd3df60e01") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330493b406bd3df60e02") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330c93b406bd3df60e03") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a331093b406bd3df60e04") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a331593b406bd3df60e05") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Mike"}); {   ... 閱讀更多

如何在 MongoDB 中使用“不匹配”運算子?

Anvi Jain
更新於 2019年7月30日 22:30:25

1K+ 次檢視

為此,請在 MongoDB 中使用 $not 運算子。為了理解這個概念,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下:> db.notLikeOperatorDemo.insertOne({"StudentName":"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a29c393b406bd3df60dfc") } > db.notLikeOperatorDemo.insertOne({"StudentName":"John Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a29cc93b406bd3df60dfd") } > db.notLikeOperatorDemo.insertOne({"StudentName":"John Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a29df93b406bd3df60dfe") } > db.notLikeOperatorDemo.insertOne({"StudentName":"Carol Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a2a1693b406bd3df60dff") } > db.notLikeOperatorDemo.insertOne({"StudentName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a2a2693b406bd3df60e00") }顯示所有文件... 閱讀更多

MongoDB 欄位名中不允許使用哪些字元?

Smita Kapse
更新於 2020年6月29日 16:00:38

692 次檢視

不要使用 $ 符號或句點 (.),因為這些字元在 MongoDB 欄位名中不允許使用。欄位不應以 $ 開頭。以下是允許使用的字元示例:> db.charactersAllowedDemo.insertOne({"Employee Name" : "John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7fefbc8d10a061296a3c6d") }使用 find() 方法顯示集合中的所有文件。查詢如下:> db.charactersAllowedDemo.find().pretty();輸出如下:{    "_id" : ObjectId("5c7fefbc8d10a061296a3c6d"),    "Employee Name" : "John" }

MongoDB 中 count() 和 find().count() 之間的區別?

Nishtha Thakur
更新於 2019年7月30日 22:30:25

334 次檢視

count() 和 find().count() 之間沒有區別。讓我們看看它們是如何工作的。為了理解這個概念,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下:> db.countDemo.insertOne({"UserId":1, "UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d278d10a061296a3c5d") } > db.countDemo.insertOne({"UserId":2, "UserName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d308d10a061296a3c5e") } > db.countDemo.insertOne({"UserId":3, "UserName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d3a8d10a061296a3c5f") } > db.countDemo.insertOne({"UserId":4, "UserName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f9d428d10a061296a3c60") }使用... 閱讀更多

查詢 MongoDB 中的陣列陣列?

Anvi Jain
更新於 2019年7月30日 22:30:25

801 次檢視

使用 $in 運算子查詢 MongoDB 中的陣列陣列。為了理解這個概念,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下:> db.arrayOfArraysDemo.insertOne({"EmployeeName":"Larry", "EmployeeSkills":[["Java", "MongoDB", "MySQL", "SQL Server"]]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f7a8d8d10a061296a3c5b") } > db.arrayOfArraysDemo.insertOne({"EmployeeName":"Mike", "EmployeeSkills":[["C", "C++"]]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f7aa68d10a061296a3c5c") }使用 find() 方法顯示集合中的所有文件。查詢如下:> db.arrayOfArraysDemo.find().pretty();輸出如下:{    "_id" : ObjectId("5c7f7a8d8d10a061296a3c5b"),    "EmployeeName" : "Larry",    "EmployeeSkills" : ... 閱讀更多

匹配 MongoDB 中包含提供的陣列的任何組合的陣列欄位?

Smita Kapse
更新於 2019年7月30日 22:30:25

204 次檢視

為此,請結合使用 `$nin` 運算子、`$elemMatch` 和 `$not`。為了理解這個概念,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下所示:-> db.combinationOfArrayDemo.insertOne({"StudentName":"Larry", "StudentAge":21, "StudentFavouriteTechnicalSubject":["C", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f77cc8d10a061296a3c58") } > db.combinationOfArrayDemo.insertOne({"StudentName":"Mike", "StudentAge":23, "StudentFavouriteTechnicalSubject":["C++", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f77dc8d10a061296a3c59") } > db.combinationOfArrayDemo.insertOne({"StudentName":"David", "StudentAge":22, "StudentFavouriteTechnicalSubject":["Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f77f48d10a061296a3c5a") }使用 `find()` 方法顯示集合中的所有文件。查詢如下:-> db.combinationOfArrayDemo.find().pretty();以下是輸出… 閱讀更多

廣告
© . All rights reserved.