【发布时间】:2015-09-09 15:26:06
【问题描述】:
现在在我的项目中,我使用的是 Play 框架 (JAVA) 和 Mongodb。我的收藏中有两个字段,分别是哈希和地址。请注意,地址字段是一个对象。
我的收藏是这样的:
db.hashed.find()
{ "_id" : ObjectId("55f04cd0d4c68ef1211e6ee4"), "hash" : "1axuhWcqKB", "address" : { "floor" : 0, "block" : "5", "city" : "Mumbai", "state" : "ABC", "pincode" : 0, "latitude" : "77.6876", "longitude" : "13.57558", "hash" : "1axuhWcqKB" } }
{ "_id" : ObjectId("55f04cd1d4c68ef1211e6ee6"), "hash" : "1ay4P407qF", "address" : { "floor" : 0, "block" : "5", "city" : "Mumbai", "state" : "ABC", "pincode" : 0, "latitude" : "77.6876", "longitude" : "13.57558", "hash" : "1ay4P407qF" } }
{ "_id" : ObjectId("55f04cd2d4c68ef1211e6ee8"), "hash" : "1ayDn3Zemh", "address" : { "floor" : 0, "block" : "5", "city" : "Mumbai", "state" : "ABC", "pincode" : 0, "latitude" : "77.6876", "longitude" : "13.57558", "hash" : "1ayDn3Zemh" } }
现在我正在考虑如何避免重复的地址条目,这显然不应该插入到数据库中。我尝试在地址字段上做 ensureindex,但看起来它不能解决这个问题。
> db.users.ensureIndex({"address" : 1},{unique: true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
希望有人可以帮助我解决这个问题。
【问题讨论】:
标签: java mongodb playframework