嵌入式关系
{
"_id" : ObjectId("603c8471f2c28d0fdf74ae78"),
"phone" : "15011226666",
"pincode" : "01-01-1991",
"name" : "bianchengbang",
"address" : [
{
"place" : "22 A, Indiana Apt",
"postcode" : 123456,
"city" : "Los Angeles",
"country" : "California"
},
{
"place" : "170 A, Acropolis Apt",
"postcode" : 456789,
"city" : "Chicago",
"country" : "Illinois"
}
]
}
这种将所有相关数据都保存在一个文档中的方式,可以使得文档的检索和维护变的更加容易,当我们要查询用户地址时,可以像下面这样:
db.users.findOne({"name":"bianchengbang"},{"address":1})
{
"_id" : ObjectId("603c8471f2c28d0fdf74ae78"),
"address" : [
{
"place" : "22 A, Indiana Apt",
"postcode" : 123456,
"city" : "Los Angeles",
"country" : "California"
},
{
"place" : "170 A, Acropolis Apt",
"postcode" : 456789,
"city" : "Chicago",
"country" : "Illinois"
}
]
}
虽然这样的结构在读取数据时很方便,但这种数据结构也是有缺点的,当用户和用户地址不断增加,数据量不断变大时,会大大影响数据库的读写性能。
所以嵌入式结构文档适用于数据不怎么变动的场景
引用式关系
{
"_id": "8296005556903116800",
"place": "北京海淀区",
"postcode": "076250",
"city": "北京",
"country": "中国",
"_class": "com.dcits.datalink.monitor.job.module.mongo.test.Address"
},
{
"_id": "8296005556903116801",
"place": "上海浦东",
"postcode": "32323223",
"city": "上海",
"country": "中国",
"_class": "com.dcits.datalink.monitor.job.module.mongo.test.Address"
},
{
"_id": "8296012815791587328",
"place": "England iland",
"postcode": "332322",
"city": "England",
"country": "英国",
"_class": "com.dcits.datalink.monitor.job.module.mongo.test.Address"
},
{
"_id": "8296012815795781632",
"place": "巴黎 街道",
"postcode": "3232222222",
"city": "巴黎",
"country": "法国",
"_class": "com.dcits.datalink.monitor.job.module.mongo.test.Address"
}
{
"_id": "8296013370126532608",
"name": "张三",
"phone": "15510514519",
"birth": {
"$date": {
"$numberLong": "1672133933180"
}
},
"address_ids": [
"8296005556903116801",
"8296005556903116800"
],
"_class": "com.dcits.datalink.monitor.job.module.mongo.test.User"
},
{
"_id": "8296013370126532609",
"name": "Lucy",
"phone": "01022112121",
"birth": {
"$date": {
"$numberLong": "1672133933180"
}
},
"address_ids": [
"8296012815791587328",
"8296012815795781632"
],
"_class": "com.dcits.datalink.monitor.job.module.mongo.test.User"
}
查询
rsb:PRIMARY> db.user.findOne({"name":"Lucy"},{"address_ids":1})
{
"_id" : "8296013370126532609",
"address_ids" : [
"8296012815791587328",
"8296012815795781632"
]
}
rsb:PRIMARY> var ids= db.user.findOne({"name":"Lucy"},{"address_ids":1})
rsb:PRIMARY> ids
{
"_id" : "8296013370126532609",
"address_ids" : [
"8296012815791587328",
"8296012815795781632"
]
}
rsb:PRIMARY> var addresses =db.address.find({"_id":{"$in":ids["address_ids"]}}).pretty()
rsb:PRIMARY> addresses
{
"_id" : "8296012815791587328",
"place" : "England iland",
"postcode" : "332322",
"city" : "England",
"country" : "英国",
"_class" : "com.dcits.datalink.monitor.job.module.mongo.test.Address"
}
{
"_id" : "8296012815795781632",
"place" : "巴黎街道",
"postcode" : "3232222222",
"city" : "巴黎",
"country" : "法国",
"_class" : "com.dcits.datalink.monitor.job.module.mongo.test.Address"
}
rsb:PRIMARY>
DBrefs
DBRef 的语法形式如下:
{ $ref : value, $id : value, $db : value }
DBRefs 中有以下三个字段:
$ref:此字段用来指定要引用文档所在的集合;
$id:此字段用来指定要引用文档的 _id 字段值;
$db:可选字段,用来指定要引用文档所在的数据库名称;
value:表示各个字段所对应的值。
在下面示例的 users 文档中,address 字段就使用了 DBRefs:
rsb:PRIMARY> db.users.insert({
... "phone":"00000000",
... "pincode":"01-01-1099",
... "address":{
... "$ref":"address",
... "$id":"8296012815791587328",
... "$db":"test"
... }})
WriteResult({ "nInserted" : 1 })
rsb:PRIMARY> db.users.find().pretty()
{
"_id" : ObjectId("63aac5be1b4920a4c76cc7c9"),
"phone" : "00000000",
"pincode" : "01-01-1099",
"address" : DBRef("address", "8296012815791587328", "test")
}
rsb:PRIMARY>
上面的 address 字段指定了引用的文档位于“bianchengbang”数据库下的“address_home”集合中,文档的 _id 值为 603c9355f2c28d0fdf74ae79。
rsb:PRIMARY> var user= db.users.findOne({phone:"00000000"})
rsb:PRIMARY> user
{
"_id" : ObjectId("63aac5be1b4920a4c76cc7c9"),
"phone" : "00000000",
"pincode" : "01-01-1099",
"address" : DBRef("address", "8296012815791587328", "test")
}
rsb:PRIMARY> var dbRef=user.address
rsb:PRIMARY> dbRef
DBRef("address", "8296012815791587328", "test")
rsb:PRIMARY> db[dbRef.$ref].findOne({"_id":(dbRef.$id)})
{
"_id" : "8296012815791587328",
"place" : "England iland",
"postcode" : "332322",
"city" : "England",
"country" : "英国",
"_class" : "com.dcits.datalink.monitor.job.module.mongo.test.Address"
}