本文主要是介绍mongodb保存数据时,字段名带点号时保存会以子文档的形式保存,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
例子
我要保存这样的数据:
{ "date" : { "$date" : "2016-10-09T16:00:00.000Z"} ,
"createtime" : { "$date" : "2016-12-27T09:36:13.305Z"} ,
"status" : 1 , "registry_sum" : 114 ,
"Go-Goal新一代V3.0" : 2 ,
"Go-Goal新一代V3.0_mobile" : null ,
"Go-Goal新一代V3.0_email" : 2 ,
"Go-Goal新一代V3.0_weixin" : null ,
"Go-Goal新一代V3.0_mobile_success" : null ,
"Go-Goal新一代V3.0_mobile_fail" : null ,
"Go-Goal新一代V3.0_email_success" : null ,
"Go-Goal新一代V3.0_email_fail" : 2 ,
"Go-Goal新一代V3.0_weixin_success" : null ,
"Go-Goal新一代V3.0_weixin_fail" : null ,
"Go-Goal新一代V3.0_mobile_success_person" : null ,
"Go-Goal新一代V3.0_mobile_fail_person" : null ,
"Go-Goal新一代V3.0_mobile_success_business" : null ,
"Go-Goal新一代V3.0_mobile_fail_business" : null ,
"Go-Goal新一代V3.0_email_success_person" : null ,
"Go-Goal新一代V3.0_email_fail_person" : 2 ,
"Go-Goal新一代V3.0_email_success_business" : null ,
"Go-Goal新一代V3.0_email_fail_business" : null ,
"Go-Goal新一代V3.0_weixin_success_person" : null ,
"Go-Goal新一代V3.0_weixin_fail_person" : null ,
"Go-Goal新一代V3.0_weixin_success_business" : null ,
"Go-Goal新一代V3.0_weixin_fail_business" : null
}
我们可以看到保存的字段例如:Go-Goal新一代V3.0
是带有点号的。
但是保存到mongodb
中就变成了子文档的显示:
"Go-Goal新一代V3" : {"0" : 2,"0_email" : 2,"0_email_fail" : 2,"0_email_fail_business" : null,"0_email_fail_person" : 2,"0_email_success" : null,"0_email_success_business" : null,"0_email_success_person" : null,"0_mobile" : null,"0_mobile_fail" : null,"0_mobile_fail_business" : null,"0_mobile_fail_person" : null,"0_mobile_success" : null,"0_mobile_success_business" : null,"0_mobile_success_person" : null,"0_weixin" : null,"0_weixin_fail" : null,"0_weixin_fail_business" : null,"0_weixin_fail_person" : null,"0_weixin_success" : null,"0_weixin_success_business" : null,"0_weixin_success_person" : null},
我们可以看到,它变成了,字段为Go-Goal新一代V3
,value
变成了子文档的形式。
那么我们该怎么访问呢?
访问子文档
db.gg_registry.find({"Go-Goal新一代V3.0":5, "Go-Goal新一代V3.0_email":2})
也就是说,访问子文档,就是字段加.
号就可以了,这与我想保存字段为Go-Goal新一代V3.0
是不冲突的。
结果为:
"Go-Goal新一代V3" : {"0" : 5,"0_email" : 2,"0_email_fail" : 2,"0_email_fail_business" : 1,"0_email_fail_person" : 1,"0_email_success" : null,"0_email_success_business" : null,"0_email_success_person" : null,"0_mobile" : 1,"0_mobile_fail" : 1,"0_mobile_fail_business" : null,"0_mobile_fail_person" : 1,"0_mobile_success" : null,"0_mobile_success_business" : null,"0_mobile_success_person" : null,"0_weixin" : 2,"0_weixin_fail" : 2,"0_weixin_fail_business" : null,"0_weixin_fail_person" : 2,"0_weixin_success" : null,"0_weixin_success_business" : null,"0_weixin_success_person" : null},
官网文档 Query查询
这篇关于mongodb保存数据时,字段名带点号时保存会以子文档的形式保存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!