【问题标题】:Explination for why map works but mongodb $each doesn't为什么 map 有效但 mongodb $each 无效的解释
【发布时间】:2017-07-11 16:35:37
【问题描述】:

谁能解释一下为什么将数组添加到我的数据库的一种方法有效而另一种无效。真的不知道有什么区别。我正在使用aldeed/node-simple-schemacollection2

例如:Schema

'topFiveSkills': { type: Array, optional: true },
'topFiveSkills.$': { type: String, optional: true }

示例:“工作示例”

topFiveSkills = ["One", "Two", "Three"]

this.state.topFiveSkills.map((skill) => {
  ProfileCandidate.update(this.state.profileCandidateCollectionId, {
    $push: { 'topFiveSkills': skill }
  });
})

示例:“不起作用”

topFiveSkills = ["One", "Two", "Three"]

ProfileCandidate.update(this.state.profileCandidateCollectionId, {
  $push: { 'topFiveSkills': { $each: [topFiveSkills] }}
});

【问题讨论】:

    标签: meteor simple-schema meteor-collection2


    【解决方案1】:

    据我所知,您在 $each 示例上应用了嵌套数组。试试下面:

    topFiveSkills = ["One", "Two", "Three"]
    
    ProfileCandidate.update(this.state.profileCandidateCollectionId, {
      $push: { 
       'topFiveSkills': { 
         $each: topFiveSkills 
       }
      }
    });
    

    请注意,我将我的对象键/值放在单独的行上。正是出于这个原因,但当然不是关键的事情。

    【讨论】:

    • 我得到了同样的结果。在集合中插入 topFiveSkills: [] 。没有数据也没有错误。
    • 你检查过profileCandidateCollectionId了吗?如果它为空,它会默默地忽略该操作。
    • 是的。我还使用验证来确保它是一个数组。它通过了验证,但是当我尝试更新它时,它现在返回not an array。我认为这是一个错误。我已经报告了,所以如果有结果我会发布结果。
    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2022-01-12
    • 2020-05-14
    • 2020-10-07
    • 2023-03-30
    相关资源
    最近更新 更多