找到 1349 篇文章 关于 MongoDB
1K+ 次浏览
要正确创建集合,您需要在调用中使用 MongoDB 对象,即 db.createCollection("yourCollectionName"); 让我们实现上述语法以创建集合并使用 MongoDB 对象调用它 −> use sample; switched to db sample > db.createCollection("employeeInformation"); { "ok" : 1 } 显示来自上述“sample”数据库的所有集合 −> db.getCollectionNames(); 这将产生以下输出 −[ "arraySizeErrorDemo", "atleastOneMatchDemo", "basicInformationDemo", "combinedAndOrDemo", "convertSQLQueryDemo", "copyThisCollectionToSampleDatabaseDemo", "countOrSizeDemo", "distinctOnMultipleFieldsDemo", "documentWithAParticularFieldValueDemo", "employee", "employeeInformation", "findListOfIdsDemo", "findMimimumElementInArrayDemo", "findSubstring", "getAllRecordsFromSourceCollectionDemo", "getElementWithMaxIdDemo", "insertDocumentWithDateDemo", "internalArraySizeDemo", ... 阅读更多
187 次浏览
让我们首先创建一个包含文档的集合 −> db.embeddedDocumentDemo.insertOne( ... { ... "CustomerDetails":[ ... {"CustomerName":"Chris", "CustomerPurchasePrice":3000}, ... {"CustomerName":"Robert", "CustomerPurchasePrice":4500}, ... {"CustomerName":"David", "CustomerPurchasePrice":1000}, ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd32347edc6604c74817ccd") } 以下是使用 find() 方法显示集合中所有文档的查询 −> db.embeddedDocumentDemo.find().pretty(); 这将产生以下输出 −{ "_id" : ObjectId("5cd32347edc6604c74817ccd"), "CustomerDetails" : [ { "CustomerName" ... 阅读更多
194 次浏览
让我们首先创建一个包含文档的集合 −> db.projectionAnElementDemo.insertOne( ... { ... "CustomerId":100, ... "CustomerDetails": [ ... { ... "CustomerName": "Chris", ... "CustomerCountryName": "US" ... }, ... { ... "CustomerName": "Robert", ... "CustomerCountryName": "UK" ... } ... ] ... } ... ); { "acknowledged" : true, ... 阅读更多
438 次浏览
您可以为此使用 $set 运算符。让我们首先创建一个包含文档的集合 −> db.updateSubObjectDemo.insertOne( ... { ... ... "ClientId" : 100, ... "ClientDetails" : { ... "ClientFirstName" : "Adam" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd31434b64f4b851c3a13e9") } 以下是使用 find() 方法显示集合中所有文档的查询 −> db.updateSubObjectDemo.find().pretty(); 这将产生以下输出 −{ "_id" : ObjectId("5cd31434b64f4b851c3a13e9"), "ClientId" : 100, "ClientDetails" : { ... 阅读更多
116 次浏览
让我们首先创建一个包含文档的集合 −>db.replacingEntireDocumentDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3119bb64f4b851c3a13e8") } 以下是使用 find() 方法显示集合中文档的查询 −> db.replacingEntireDocumentDemo.find().pretty(); 这将产生以下输出 −{ "_id" : ObjectId("5cd3119bb64f4b851c3a13e8"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentCountryName" : "US" } 以下是替换整个文档的同时更新 MongoDB 文档的查询 −>db.replacingEntireDocumentDemo.update({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentCountryName":"US"}, {"StudentFirstName":"David", "StudentLastName":"Miller", "StudentCountryName":"AUS"}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 让我们显示集合中的所有记录 ... 阅读更多
823 次浏览
使用 unique 并将其设置为 TRUE。让我们通过创建索引并将两列设置为唯一来实现相同的目的 −>db.keepTwoColumnsUniqueDemo.createIndex({"StudentFirstName":1, "StudentLastName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } 现在您可以在上述集合中插入文档 −>db.keepTwoColumnsUniqueDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30fd7b64f4b851c3a13e5") } >db.keepTwoColumnsUniqueDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30fe5b64f4b851c3a13e6") } >db.keepTwoColumnsUniqueDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentAge":24}); 2019-05-08T22:50:42.803+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: sample.keepTwoColumnsUniqueDemo index: StudentFirstName_1_StudentLastName_1 dup key: { : "John", : "Smith" } ... 阅读更多
482 次浏览
使用 delete 运算符在 MongoDB shell 中取消设置变量。以下是语法 −delete yourVariableName; 让我们现在实现上述语法以在 MongoDB shell 中取消设置变量。首先,打印变量名 −> customerDetail; 这将产生以下输出 −2019-05-08T22:29:17.361+0530 E QUERY [js] ReferenceError: customerDetail is not defined : @(shell):1:1 现在您可以在上述变量中设置值。以下是查询 −> customerDetail={"CustomerFirstName":"Chris"}; 这将产生以下输出 −{ "CustomerFirstName" : "Chris" } 以下是显示变量值的查询 −> customerDetail; 这将产生以下输出 −{ "CustomerFirstName" : "Chris" } 以下 ... 阅读更多
1K+ 次浏览
要在 MongoDB 中组合 AND & OR,让我们首先创建一个包含文档的集合 −>db.combinedAndOrDemo.insertOne({"StudentFirstName":"John", "StudentAge":23, "StudentSkill":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd306dcb64f4b851c3a13e2") } >db.combinedAndOrDemo.insertOne({"StudentFirstName":"Larry", "StudentAge":21, "StudentSkill":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd306f3b64f4b851c3a13e3") } >db.combinedAndOrDemo.insertOne({"StudentFirstName":"Sam", "StudentAge":24, "StudentSkill":"SQL Server"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30701b64f4b851c3a13e4") } 以下是使用 find() 方法显示集合中所有文档的查询 −> db.combinedAndOrDemo.find().pretty(); 这将产生以下输出 −{ "_id" : ObjectId("5cd306dcb64f4b851c3a13e2"), "StudentFirstName" : "John", "StudentAge" : 23, "StudentSkill" : "MongoDB" } { "_id" : ... 阅读更多
210 次浏览
使用DISTINCT获取唯一元素,然后使用length获取数组大小:−db.yourCollectionName.distinct('yourFieldName').length; 让我们先创建一个包含文档的集合:−> db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd304f5b64f4b851c3a13dc") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd304fab64f4b851c3a13dd") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd304fcb64f4b851c3a13de") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30500b64f4b851c3a13df") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30505b64f4b851c3a13e0") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3050ab64f4b851c3a13e1") }以下是显示所有...的查询 阅读更多
2K+ 次浏览
要重命名用户,需要使用update()和$set设置新的用户名。以下是语法:−db.system.users.update({"user":"yourOldUserName"}, {$set:{"user":"yourNewUserName"}});首先,显示MongoDB数据库中的所有用户:−> use admin; switched to db admin > db.getUsers(); 这将产生以下输出:−[ { "_id" : "admin.Chris", "user" : "Chris", "db" : "admin", "roles" : [ { "role" : "readWrite", "db" : "test" } ... 阅读更多