0

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

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;
    }
}

_partition我修改and没有问题category,但是由于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 修改数组?

4

1 回答 1

0

我最终在 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/

于 2020-10-26T21:16:56.087 回答