在MongoDB中,多个位置标识符最经济的替代方法是什么? [英] What's the most economical alternative to multiple positional identifiers in MongoDB?

查看:65
本文介绍了在MongoDB中,多个位置标识符最经济的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 authors 的集合,该集合具有以下架构:

I have a collection named authors with the following schema:

authors: {
  _id,
  firstName,
  lastName,
  posts: [
    post 1: {...},
    post 2: {...},
    post 3: {
      _id,
      title,
      content,
      tags: [
        tag 1: {...},
        tag 2: {...},
        tag 3: {
          _id,
          name,
          description,
        },
      ],
    },
  ],
}

可以看出,帖子 authors 集合中的一组对象.反过来,此数组中的每个对象都有标签,这是另一个对象数组.每个标签对象都有三个字段: _id 名称描述.

As can be seen, posts is an array of objects inside the authors collection. Each object inside this array, in turn, has tags, another array of objects. And each of these tags objects has three fields: _id, name, and description.

我正在尝试编写一个GraphQL变体来更新集合中匹配文档上的 name 字段.

I'm trying to write a GraphQL mutation to update this name field on matching documents in the collection.

    const updatedTagInAuthor = await Author
  .updateMany({ 'posts.tags._id': args.newTagInfo._id },
    {
      $set: {
        'posts.$.tags.$.name': args.newTagInfo.name,
        'posts.$.tags.$.description': args.newTagInfo.description,
      },
    }, opts);

以上代码段显然失败了,因为MongoDB不允许查询中包含多个位置元素( $ ).那么,有没有经济的选择可以完成我想做的事情?

The above snippet obviously fails since MongoDB doesn't allow multiple positional elements ($) in a query. So is there any economical alternative to accomplish what I'm trying to do?

我尝试了MongoDB建议的 ArrayFilter 方法:

I tried the ArrayFilter method as MongoDB suggests:

           const updatedTagInAuthor = await Author.update(
              {},
              { $set: { 'posts.$[].tags.$[tag].name': args.newTagInfo.name } },
              { arrayFilters: [{ 'tag._id': args.newTagInfo._id }], multi: true }
            );

但这会引发以下错误:

无法读取未定义的属性"castForQuery"

Cannot read property 'castForQuery' of undefined

还是很困惑!

推荐答案

这些是我使用给定的查询类型正在更新的文档,

These are the documents I'm updating with the kind of query I have given,

{"name" : "Steve","details" : [ {
        "work" : [ {
                "Company" : "Byjus",
                "id" : 1,
                "country" : "India"
            }, 
            {
                "Company" : "Vodafone",
                "id" : 2,
                "country" : "UK"
            }]
    }]},{"name" : "Virat","details" : [ {
        "work" : [ {
                "Company" : "Byjus",
                "id" : 1,
                "country" : "India"
            }, 
            {
                "Company" : "Verizon",
                "id" : 3,
                "country" : "US"
            }]
    }]}

QUERY:

db.getCollection('Users').update({"details": {"$elemMatch": {"work.id": 1}}}, {'$set': {'details.$[].work.$.Company': 'Boeing', 'details.$[].work.$.country': 'US'} }, {multi: true});

这类似于您所问的对吗? 尝试将这两个文档插入名为User的集合中,然后直接在Mongo CONSOLE中而不是在GUI中尝试上述查询.完全使用查询,而不仅仅是$ set方法.

It's similar to what you asked right? Try inserting those two Documents in a collection called User and try the above query in Mongo CONSOLE directly, not in GUI. Use the Query completely not just the $set method.

这篇关于在MongoDB中,多个位置标识符最经济的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆