Swift Perfect-MongoDB如何通过主键"
2017-10-23 本文已影响169人
股金杂谈
图片.png
let phoneBson = BSON()
var oid = bson_oid_t()
bson_oid_init_from_string(&oid, userID)
phoneBson.append(key: "_id", oid: oid)
let fnd: MongoCursor? = userCollection?.find(query: phoneBson)
let newBson = BSON(map:self.getJSONValues())
defer {
phoneBson.close()
newBson.close()
ESLog.debug(message: "phoneBson.close()")
}
let findObj = fnd?.next()
if findObj == nil {//如果没有找到, 返回错误
ESLog.error(message: "修改用户信息时, 用户不存在, \(userID)")
return MongoResult.error(0, 0, "用户不存在")
} else {//如果找到到了更新数据
let result = userCollection?.update(selector: (findObj)!, update: newBson, flag: .upsert)
return result!
}