对于以_开头的MongoDB集合,以下是语法-
db.createCollection(‘_yourCollectionName’);
使用以下语法插入查询-
db.getCollection('_yourCollectionName').insertOne({"yourFieldName1":"yourValue1","yourFieldName2":yourValue2,............N});
首先让我们创建一个包含文档的集合-
> db.createCollection('_testUnderscoreCollectionDemo'); { "ok" : 1 } >db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"John","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb4a6140b992277dae0e4") } >db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"Carol","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb4af140b992277dae0e5") }
以下是在find()方法的帮助下显示集合中所有文档的查询-
> db.getCollection('_testUnderscoreCollectionDemo').find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5ccfb4a6140b992277dae0e4"), "StudentFirstName" : "John", "StudentAge" : 23 } { "_id" : ObjectId("5ccfb4af140b992277dae0e5"), "StudentFirstName" : "Carol", "StudentAge" : 21 }