Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 使用upsert和$all更新_Mongodb - Fatal编程技术网

Mongodb 使用upsert和$all更新

Mongodb 使用upsert和$all更新,mongodb,Mongodb,我无法让它工作: db.library.update({ categories: { $all: ['/movie/action', '/movie/comedy'], $nin: ['/movie/cartoon'] }, location: { $geoWithin: { $centerSphere: [[48.8574946, 2.3476296000000048], 50/6378.1] } } }, { $setOnIns

我无法让它工作:

db.library.update({
  categories: {
    $all: ['/movie/action', '/movie/comedy'],
    $nin: ['/movie/cartoon']
  },
  location: {
    $geoWithin: {
      $centerSphere: [[48.8574946, 2.3476296000000048], 50/6378.1]
    }
  }
},
{
  $setOnInsert: {
      categories: ['/movie/action', '/movie/comedy'],
      location: {
          type: 'Point',
          coordinates: [48.8574946, 2.3476296000000048]
      }
  },
  $addToSet: {users: {_id: '1', date: '2018-04-06'}}
},
{ upsert: true })
它返回以下错误:

cannot infer query fields to set, path 'categories' is matched twice
我知道,当发生upsert时,查询部分被移动到更新部分,但我不确定如何防止
$all
产生这种效果


$all
数组未设置为超过1个元素时,它确实起作用。

我找到了这个解决方案,尽管被迫在
{$elemMatch:{$eq:…
下列出
$all
元素很痛苦:

db.library.update({
  categories: {
        $all: [
            { $elemMatch: { $eq: '/movie/action' } },
            { $elemMatch: { $eq: '/movie/comedy' } }
        ],
        $nin: ['/movie/cartoon']
  },
  location: {
    $geoWithin: {
      $centerSphere: [[48.8574946, 2.3476296000000048], 50/6378.1]
    }
  }
},
{
  $setOnInsert: {
      categories: ['/movie/action', '/movie/comedy'],
      location: {
          type: 'Point',
          coordinates: [48.8574946, 2.3476296000000048]
      }
  },
  $addToSet: {users: {_id: '1', date: '2018-04-06'}}
},
{ upsert: true })
欢迎任何简单的解决方案