找到关于数据库的6705 篇文章
680 次浏览
对于此类评估,请在 MongoDB 中使用 aggregate()。让我们创建一个包含文档的集合 −> db.demo90.insertOne( ... {"words": ["john", "jace"] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c1ada79799acab037af56") } > db.demo90.insertOne( ... {"words": ["sam", "adam"] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c1adb79799acab037af57") } 使用 find() 方法显示集合中的所有文档 −> db.demo90.find(); 这将产生以下输出 −{ "_id" : ObjectId("5e2c1ada79799acab037af56"), "words" : [ "john", "jace" ] } { "_id" : ObjectId("5e2c1adb79799acab037af57"), "words" : [ "sam", "adam" ] } 以下是查找文档的查询 ... 阅读更多
242 次浏览
要在 MongoDB 中获取嵌套字符串数组,请在 find() 中使用点表示法。让我们创建一个包含文档的集合 −> db.demo89.insertOne( ... { id: 101, Details: [ { Name: "Chris", Marks: 45 }, { Name: "David", Marks: 55, Subjects : ["MySQL", "MongoDB", "Java", "C"] } ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c163b79799acab037af51") } > db.demo89.insertOne( ... { id: 102, Details: [ { Name: "Mike", Marks: 48 }, { Name: "Bob", Marks: 98, Subjects : ["C++", "MySQL"] } ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c163c79799acab037af52") } 显示 ... 阅读更多
74 次浏览
是的,您可以使用 $indexOfCP 将字段值用作模式。让我们创建一个包含文档的集合 −> db.demo88.insertOne( ... { ... "Name": "Chris", ... "PassoutYear": "2020", ... "websiteName": "chris.shop.com/Carol-2020-" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c14c471bf0181ecc422b1") } > db.demo88.insertOne( ... { ... "Name": "David", ... "PassoutYear": "2010", ... "websiteName": "david.bigshop.com/David-2010-" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c14c571bf0181ecc422b2") } 显示集合中的所有文档 ... 阅读更多
108 次浏览
要按多个数组元素过滤,请使用 $elemMatch。让我们创建一个包含文档的集合 −> db.demo87.insertOne( ... { ... id:101, ... "Details": [ ... { ... "EmployeeName": "Chris", ... "Salary": 45000 ... }, ... { ... "EmployeeName": "David", ... "Salary": 50000 ... } ... ] ... } ... ); { "acknowledged" ... 阅读更多
707 次浏览
为此,请使用 $not 和 $in。让我们创建一个包含文档的集合 −[ { id: "101", subjectid: [ "102" ] }, { id: "102", subjectid: [ "102" ] } ] 这是快照。使用 find() 方法显示集合中的所有文档 −db.collection.find() 这将产生以下输出 −[ { "_id": ObjectId("5a934e000102030405000000"), "id": "101", "subjectid": [ "102" ] }, { "_id": ObjectId("5a934e000102030405000001"), "id": "102", "subjectid": [ "102" ] } ] 以下是使用 $expr、$not 和 $in 来获取排除匹配字段数组值的查询 −db.collection.find({ $expr: { $not:{ $in: [ "$id", "$subjectid" ] } } }) 这将产生以下输出 −[ { "_id": ObjectId("5a934e000102030405000000"), "id": "101", "subjectid": [ "102" ] } ]
893 次浏览
要匹配数组内的多个条件,请使用 aggregate()。让我们创建一个包含文档的集合 −> db.demo84.insertOne({ ... "EmployeeDetails": [ ... {Name: 'John', Salary:45000, isMarried: true}, ... {Name: 'Chris', Salary:50000, isMarried: false} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c0a3471bf0181ecc422a5") } > db.demo84.insertOne({ ... "EmployeeDetails": [ ... {Name: 'Sam', Salary:56000, isMarried: false}, ... {Name: 'Bob', Salary:50000, isMarried: false} ... ] ... } ... ); { "acknowledged" : true, ... 阅读更多
994 次浏览
要在 MongoDB 中创建一个唯一字段,请使用 unique − true。让我们创建一个包含文档的集合 −> db.demo82.createIndex({"EmployeeName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo82.insertOne({"EmployeeName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bfb1b71bf0181ecc422a0") } > db.demo82.insertOne({"EmployeeName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bfb1f71bf0181ecc422a1") } > db.demo82.insertOne({"EmployeeName":"Chris"}); 2020-01-25T13:54:00.410+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo82 index: EmployeeName_1 dup key: { : "Chris" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key ... 阅读更多
112 次浏览
要获取子文档中的特定项目,可以使用点(.)表示法。让我们创建一个包含文档的集合:−> db.demo81.insertOne({"StudentDetails":[{"StudentName":"Carol", "StudentSubject":"Java"}, { "StudentName" : "David", "StudentSubject" : "MongoDB" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf6ec71bf0181ecc4229d") } > db.demo81.insertOne({"StudentDetails":[{"StudentName":"Mike", "StudentSubject":"Python"}, { "StudentName" : "David", "StudentSubject" : "C" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf70471bf0181ecc4229e") } > db.demo81.insertOne({"StudentDetails":[{"StudentName":"Jace", "StudentSubject":"C++"}, { "StudentName" : "John", "StudentSubject" : "MySQL" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf72071bf0181ecc4229f") } 使用find()方法显示集合中的所有文档 −> db.demo81.find(); 这将产生以下结果... 阅读更多
421 次浏览
要在 MongoDB 中计算平均值,请使用 $avg。让我们创建一个包含文档的集合:−> db.demo80.insertOne({"Details":{"Price":10.5}}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf43271bf0181ecc42297") } > db.demo80.insertOne({"Details":{"Price":50.3}}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf43871bf0181ecc42298") } > db.demo80.insertOne({"Details":{"Price":100.10}}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf43f71bf0181ecc42299") } 使用find()方法显示集合中的所有文档 −> db.demo80.find(); 这将产生以下输出:−{ "_id" : ObjectId("5e2bf43271bf0181ecc42297"), "Details" : { "Price" : 10.5 } } { "_id" : ObjectId("5e2bf43871bf0181ecc42298"), "Details" : { "Price" : 50.3 } } { "_id" : ObjectId("5e2bf43f71bf0181ecc42299"), "Details" : { "Price" ... 阅读更多