MongoError:虚线字段..对存储无效

Tar*_*len 24 schema mongodb meteor

我正在尝试使用以下查询更新具有匹配嵌套属性的文档

upsertByCommentThreadId: function(commentThread) {
    return CommentThreads.update({
         'youtube.commentThreadId': commentThread.youtube.commentThreadId 
      },
      {
        $set: commentThread
      },
      {
        upsert: true
      }
    );
  }
Run Code Online (Sandbox Code Playgroud)

架构:

Schema({
  youtube: {
    type: Object
  },
  'youtube.etag': {
    type: String
  },
  'youtube.commentThreadId': {
    type: String,
    index: 1
  },
  ...
Run Code Online (Sandbox Code Playgroud)

但是我收到了一个错误

Exception while invoking method ... MongoError: The dotted field 'youtube.commentThreadId' in 'youtube.commentThreadId' is not valid for storage.
Run Code Online (Sandbox Code Playgroud)

我不知道如果不是通过点表示法我还能查询嵌套属性

小智 10

这是mongo 3.6之前对字段名称的限制,现在你可以将mongo服务器更新到3.6或更高版本来解决这个问题.

https://docs.mongodb.com/manual/reference/limits/#naming-restrictions


Ger*_*erd 7

发生错误是因为当密钥中存在点时,MongoDB无法工作.一个类似的错误,

MongoError: The dotted field '2016.11.14' in 'myData.days.2016.11.14' is
            not valid for storage.
Run Code Online (Sandbox Code Playgroud)

当代码是:

day = "2016.11.14"
myData.days[ day ] = 11
Run Code Online (Sandbox Code Playgroud)

当数据更改为

day = "2016_11_14"
Run Code Online (Sandbox Code Playgroud)

问题是固定的.


Zla*_*tko 3

您需要展平您的$set运算符参数:

schema.update( {
  'youtube.commentThreadId': commentThread.youtube.commentThreadId
},
{
  $set: {
    // Here is my change
    'youtube.commentThreadId': commentThread.youtube.commentThreadId
  }
},
{ /* opts */});
Run Code Online (Sandbox Code Playgroud)

您没有显示您的commentThread(该upsertByCommentThreadId()函数的输入参数 - 但我怀疑您正在发送对象,根据该查询部分判断。例如,您有:

let commentThread = {
  youtube: {
    commentThreadId: 12345
  }
};
Run Code Online (Sandbox Code Playgroud)

所以你的查询部分没问题,但$set需要一个简单的键:值,其中键必须是字符串。

因此,您发送的内容如下:

CommentThreads.update({

     // Here it's ok, as it's translated to:
     // { 'youtube.commentThreadId': 12345 }
     'youtube.commentThreadId': commentThread.youtube.commentThreadId 
  },
  {

    // but this gets expanded to a deeply nested object, like:
    // { $set: { youtube: {commentThreadId: 12345}}} 
    $set: commentThread
  },
  {/* opts */}
);
Run Code Online (Sandbox Code Playgroud)

因此,您的$set操作员需要一个简单的{ key: value },其中key是一个字符串。