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 })
欢迎任何简单的解决方案