美文网首页
Swift Perfect-MongoDB如何通过主键"

Swift Perfect-MongoDB如何通过主键"

作者: 股金杂谈 | 来源:发表于2017-10-23 19:50 被阅读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!
        }

相关文章

网友评论

      本文标题:Swift Perfect-MongoDB如何通过主键"

      本文链接:https://www.haomeiwen.com/subject/eptzuxtx.html