【问题标题】:mongoDB iOS Swift SDK modify array?mongoDB iOS Swift SDK 修改数组?
【发布时间】:2020-10-26 11:58:20
【问题描述】:

我的数据模型对象在这里:

class Tiktoks: Object {
    @objc dynamic var _id: ObjectId = ObjectId.generate()
    @objc dynamic var _partition: String = ""
    @objc dynamic var category: String = ""
    @objc dynamic var date: Date = Date()
    let tiktoks = RealmSwift.List<String>()
    override static func primaryKey() -> String? {
        return "_id"
    }
    
    convenience init(partition: String, category: String) {
        self.init()
        self._partition = partition;
        self.category = category;
    }
}

我修改_partitioncategory 没有问题,但是由于tiktoks 列表是由let 定义的,如果我将它放在convenience init 函数中让我能够修改它。我尝试将@objc dynamic var 放在它前面,但它显示的是Property cannot be marked @objc because its type cannot be represented in Objective-C

有没有办法使用 mongoDB iOS Swift SDK 修改数组?

【问题讨论】:

    标签: ios swift mongodb mongodb-realm


    【解决方案1】:

    我最终在 MongoDB Realm 中创建了一个函数。

    exports = function(category, url){
      
      var collection = context.services.get("mongodb-atlas").db("database").collection("collection")
      
      collection.updateOne(
        { category: category },
        { 
          $push: { 
              tiktoks: {
              $each: [ url ], 
              $position: 0
            }
          },
          $set: {
            date: new Date()
          }
        },
        )
      
      return "done"; // not supposed to return string I think, has to return "non-double" result
    };
    

    它可以使用:

    user.functions.add_tiktok_to_category([AnyBSON(stringLiteral: tiktoks.category), AnyBSON(stringLiteral: $0!)])
    

    这是在 Swift 中运行 MongoDB 函数的文档:https://docs.mongodb.com/realm/ios/call-a-function/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 2014-08-23
      • 1970-01-01
      • 2011-03-20
      相关资源
      最近更新 更多