当被删除的键名称中包含点时如何使用$unset?

问题描述 投票:0回答:1

当按键名称中包含点时如何使用

$unset

const key = "test"
const result = await db.collection(collection).updateOne(
    { [key]: { $exists: true } },
    { $unset: { [pathToKey]: 1 } }
);

鉴于此文件:

{
  "test": {
    "a": "b",         // <- test.a successfully erases the 'a' key
    "v0.0.1": "ok"    // <- test.v0.0.1 fails
  }
}

pathToKey
'test.a'
时,成功擦除
a

当它

'test.v0.0.1'
时失败,可能是因为密钥名称中包含
.

我使用的是最新的Mongo版本

https://mongoplayground.net/p/Chiy7DtIqWb

node.js mongodb mongodb-query
1个回答
0
投票

MongoDB不支持带点的键 http://docs.mongodb.org/manual/reference/limits/#Restrictions-on-Field-Names

也许你可以使用解决方法,但我认为使用不同的键会更好

© www.soinside.com 2019 - 2024. All rights reserved.