找到 1349 篇文章 關於 MongoDB
942 次瀏覽
這裡,$push 可用於在巢狀陣列中新增新文件。為了理解上述 $push 概念,讓我們建立一個包含巢狀陣列文件的集合。建立包含文件的集合的查詢如下:>db.nestedArrayDemo.insertOne({"EmployeeName":"Larry", "EmployeeSalary":9000, "EmployeeDetails": [{"EmployeeDOB":new Date('1990-01-21'), "EmployeeDepartment":"ComputerScience", "EmployeeProject": [{"Technology":"C", "Duration":6}, {"Technology":"Java", "Duration":7}]}]});以下是輸出:{ "acknowledged" : true, "insertedId" : ObjectId("5c6d73090c3d5054b766a76e") }現在,您可以使用 find() 方法顯示集合中的文件。查詢如下:> db.nestedArrayDemo.find().pretty();以下是輸出:{ "_id" : ObjectId("5c6d73090c3d5054b766a76e"), "EmployeeName" : "Larry", "EmployeeSalary" : 9000, "EmployeeDetails" ... 閱讀更多
623 次瀏覽
您可以使用 length 來查詢陣列大小大於 1 的文件:db.yourCollectionName.find({$where:"this.yourArrayDocumentName.length > 1"}).pretty();為了理解上述語法,讓我們建立一個包含一些文件的集合。建立包含文件的集合的查詢如下:>db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Larry", "StudentTechnicalSubje ct":["Java", "C", "C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d6c4c0c3d5054b766a76a") } >db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Maxwell", "StudentTechnicalSu bject":["MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d6c660c3d5054b766a76b") } >db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Maxwell", "StudentTechnicalSu bject":["MySQL", "SQL Server", "PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d6c800c3d5054b766a76c") }使用 find() 方法顯示集合中的所有文件。查詢如下:> db.arrayLengthGreaterThanOne.find().pretty();以下... 閱讀更多
650 次瀏覽
要更新文件陣列中的物件,您需要使用 update() 方法。為了理解 update() 方法,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下:> db.updateObjects.insertOne({"CustomerId":1, "CustomerName":"Larry", "TotalItems":100, ... "ItemDetails":[ ... { ... "NameOfItem":"Item_1", ... "Amount":450 ... }, ... { ... "NameOfItem":"Item_2", ... "Amount":500 ... }, ... { ... "NameOfItem":"Item_3", ... "Amount":200 ... } ... ] ... } ... );以下是輸出:{ "acknowledged" : true, "insertedId" : ObjectId("5c6d688b0c3d5054b766a769") }現在,您可以使用 find() 方法顯示集合中的文件。查詢... 閱讀更多
346 次瀏覽
您可以在應用匹配之前使用聚合並解開陣列列表。為了理解上述概念,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下:> db.filterArray.insertOne( { "L": [{ "N":1 }, { "N":2 } , { "N":3 }, { "N":4 }, { "N":5 } ]});執行上述查詢後,以下內容可見:{ "acknowledged" : true, "insertedId" : ObjectId("5c6d63f2734e98fc0a434aeb") }使用 find() 方法顯示集合中的文件。查詢如下:> db.filterArray.find().pretty();以下是輸出:{ "_id" : ObjectId("5c6d63f2734e98fc0a434aeb"), "L" : [ ... 閱讀更多
1K+ 次瀏覽
要從 MongoDB 獲取隨機記錄,您可以使用聚合函式。語法如下:db.yourCollectionName.aggregate([{$sample:{size:1}}]);為了理解上述語法,讓我們建立一個包含一些文件的集合。建立集合的查詢如下:>db.employeeInformation.insert({"EmployeeId":1, "EmployeeName":"Maxwell", "EmployeeAge":26}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":2, "EmployeeName":"David", "EmployeeAge":25}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":3, "EmployeeName":"Carol", "EmployeeAge":24}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":4, "EmployeeName":"Bob", "EmployeeAge":28}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":5, "EmployeeName":"Sam", "EmployeeAge":27); WriteResult({ "nInserted" : 1 })現在,您可以使用 find() 方法顯示集合中的所有文件。查詢如下:> db.employeeInformation.find().pretty();以下是輸出:{ ... 閱讀更多
4K+ 次瀏覽
您可以使用 $where 運算子以及 find() 方法來比較 MongoDB 中的兩個欄位。語法如下:db.yourCollectionName.find({$where:”yourCondition”}).pretty();為了理解上述語法,讓我們建立一個包含一些文件的集合。建立包含文件的集合的查詢如下:>db.compareTwoFields.insert({"Id":1, "FullName":{"FirstName":"John", "LastName":"Smith"}, "BranchName":"ComputerScience"}); WriteResult({ "nInserted" : 1 }) >db.compareTwoFields.insert({"Id":2, "FullName":{"FirstName":"Smith", "LastName":"Smith"}, "BranchName":"Civil"}); WriteResult({ "nInserted" : 1 }) >db.compareTwoFields.insert({"Id":3, "FullName":{"FirstName":"David", "LastName":"Smith"}, "BranchName":"Mechanical"}); WriteResult({ "nInserted" : 1 })現在,您可以使用 find() 方法顯示集合中的所有文件。查詢如下:> db.compareTwoFields.find().pretty();以下是輸出:{ "_id" : ObjectId("5c6c237568174aae23f5ef64"), ... 閱讀更多
1K+ 次瀏覽
獲取集合中所有鍵的名稱的語法如下:var yourVariableName1=db.yourCollectionName.findOne(); for(var yourVariableName 2 in yourVariableName1) { print(yourVariableName); }為了理解上述語法,讓我們建立一個包含文件的集合。我們正在建立的集合名稱為“studentGetKeysDemo”。以下是建立文件的查詢:>db.studentGetKeysDemo.insert({"StudentId":1, "StudentName":"Larry", "StudentAge":23, "StudentAddress":"US", ... "StudentHobby":["Cricket", "Football", "ReadingNovel"], "StudentMathMarks":89, "StudentDOB":ISODate('1998-04-06')});以下是輸出:WriteResult({ "nInserted" : 1 })使用 find() 方法顯示集合中的所有文件。查詢如下:> db.studentGetKeysDemo.find().pretty();以下是輸出:{ "_id" : ObjectId("5c6c12dd68174aae23f5ef5f"), "StudentId" : 1, "StudentName" : ... 閱讀更多
2K+ 次瀏覽
您可以輕鬆地使用“like”查詢 MongoDB:db.yourCollectionName.find({"yourFieldName" : /.*yourMatchingValue.*/}).pretty();為了理解上述語法,讓我們建立一個包含一些文件的集合。這裡,我們有一個名為“employee”的集合。查詢如下:> db.employee.insert({"EmployeeName":"John", "EmployeeSalary":450000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Carol", "EmployeeSalary":150000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"James", "EmployeeSalary":550000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Jace", "EmployeeSalary":670000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Larry", "EmployeeSalary":1000000}); WriteResult({ "nInserted" : 1 })現在,您可以使用 find() 方法顯示集合中的所有文件。查詢如下:> db.employee.find().pretty();以下是輸出:{ "_id" : ObjectId("5c6c0b2e68174aae23f5ef59"), ... 閱讀更多
3K+ 次瀏覽
使用運算子 $gte 和 $lt 在 MongoDB 中查詢兩個日期之間的物件。為了理解這些運算子,讓我們建立一個集合。在這裡建立一個集合:>db.order.insert({"OrderId":1, "OrderAddrees":"US", "OrderDateTime":ISODate("2019-02-19")}; WriteResult({ "nInserted" : 1 }) >db.order.insert({"OrderId":2, "OrderAddrees":"UK", "OrderDateTime":ISODate("2019-02-26")}; WriteResult({ "nInserted" : 1 })使用 find() 方法顯示集合中的所有文件。查詢如下:> db.order.find().pretty();以下是輸出:{ "_id" : ObjectId("5c6c072068174aae23f5ef57"), "OrderId" : 1, "OrderAddrees" : "US", "OrderDateTime" : ISODate("2019-02-19T00:00:00Z") } { "_id" : ObjectId("5c6c073568174aae23f5ef58"), "OrderId" : 2, "OrderAddrees" : "UK", "OrderDateTime" : ISODate("2019-02-26T00:00:00Z") }以下是查詢... 閱讀更多
2K+ 次瀏覽
您可以使用聚合函式來更新 MongoDB 欄位,使用另一個欄位的值。這裡,我們將建立兩個集合:姓名學生資訊集合建立包含文件的第一個集合的查詢如下:> db.name.insert({"FirstName":"John", "LastName":"Smith"}); WriteResult({ "nInserted" : 1 })現在,您可以使用 find() 方法顯示集合中的所有文件。查詢如下:> db.name.find().pretty();以下是顯示集合“name”文件的輸出:{ "_id" : ObjectId("5c6c00dd68174aae23f5ef55"), "FirstName" : "John", "LastName" : "Smith" } 集合建立包含文件的第二個集合的查詢如下:> db.studentInformation.insert({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); WriteResult({ "nInserted" : 1 })現在 ... 閱讀更多
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP