search

Home  >  Q&A  >  body text

在 Node.js 中,如何向 MongoDB 数据库中储存一个 JavaScript 对象

我希望在 MongoDB 中储存一个 Mongo 查询,比如可能类似下面这样:

{
    $set: {
        account_id: ObjectID('525284cc2cebb6d0008b4567')
    }
}

但是我并不能直接储存在 MongoDB 中,因为 $set 在 MongoDB 中是一个不合法的属性名。

而且也不能用 JSON 序列化,因为 ObjectID 这种对象序列化之后没法被正确地还原。

PHPzPHPz2853 days ago583

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-17 11:14:29

    Escape loses the string...how to unescape it back when needed

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 11:14:29

    Why don’t you just save the ID and spell out the query yourself next time?

    reply
    0
  • 阿神

    阿神2017-04-17 11:14:29

    What about BSON serialization?

    reply
    0
  • Cancelreply