mongoDB的使用

命令行模糊查找-使用正则表达式

db.集合名.find({"key":"/.3./"}).pretty(); // 加上 .pretty() 的目的是使数据库中显示出来的数据格式化

格式化数据显示

加上pretty() : db.集合名.find({"key":"/.3./"}).pretty();

向嵌套的数据集合中插入数据 $push

db.objects.update({"pid": 103}, {   $push: {   
                                      "comments": {       
                                                     "pid": 103,         
                                                     "comid": 1,        
                                                     "uid": 1,      
                                                     "comContents": "我不管,我最美,啊哈哈哈哈"      
                                                  }   
                                           }
                                 }
                  );
mongoDB的使用_第1张图片
执行上述嵌套代码之前
mongoDB的使用_第2张图片
执行上述嵌套代码之后

结论:你瞧你瞧 !!!, 如果一个集合中没有某一个属性,那么向这个集合中插入某一个属性,且这个属性是一个对象,则插入后会默认为一个数组

删除嵌套数据集合中的某一个属性领域: $unset

The $unset operator deletes a particular field. Consider the following syntax:
{ $unset: { : "", ... } }
Example
The following update()
operation uses the $unset operator to remove the fields quantity andinstock from the first document in the products collection where the field sku has a value of unknown
.```
db.products.update(
{ sku: "unknown" },
{ $unset: { quantity: "", instock: "" } }
)

实际用法:

db.objects.update({"pid": 103}, {  
 $unset: {      "comments": [        
                               {  "pid": 103,          
                                  "comid": 1,            
                                  "uid": 1,           
                                  "comContents": "我不管,我最美,啊哈哈哈哈"   
                               } 
                            ] 
         }
});

直接在命令行执行或者写成js脚本执行上述代码,就可以删除集合中的comments属性:

mongoDB的使用_第3张图片
删除的过程,和删除后的结果

你可能感兴趣的:(mongoDB的使用)