小编5a0*_*01P的帖子

更新数组中的元素如果存在,则在MongoDb中的该数组中插入新元素

我在MongoDB中有一组元素数组,如下所示:

{ 
    "_id" : 5, 
    "quizzes" : [
        {
            "wk" : 1, 
            "score" : 10
        }, 
        {
            "wk" : 2, 
            "score" : 8
        }, 
        {
            "wk" : 3, 
            "score" : 5
        }
      ], 
    "play" : [
        {
            "wk" : 2, 
            "score" : 8
        }, 
        {
            "wk" : 3, 
            "score" : 5
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试在数组中插入新记录(如果不存在),如果该数组中存在记录,则更新该数组记录.下面是我的MongoDB查询.

db.push.update(
    { _id: 5 },
    { $push: { "quizzes": {"wk" : 6.0,"score" : 8.0},"play": {"wk" : 6.0,"score" : 8.0}  } }
)
Run Code Online (Sandbox Code Playgroud)

每当我执行此查询时,它会在数组中插入新记录但我想如果记录存在则更新该数组.

arrays mongodb

6
推荐指数
1
解决办法
2321
查看次数

数组内的多个嵌套组

我在 MongoDB 中有一组元素,如下所示:

/* 1 */
{
    "_id" : ObjectId("58736c7f7d43c305461cdb9b"),
    "Name" : "Kevin",
    "pb_event" : [ 
        {
            "event_type" : "Birthday",
            "event_date" : "2014-08-31"
        }, 
        {
            "event_type" : "Anniversary",
            "event_date" : "2014-08-31"
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("58736cfc7d43c305461cdba8"),
    "Name" : "Peter",
    "pb_event" : [ 
        {
            "event_type" : "Birthday",
            "event_date" : "2014-08-31"
        }, 
        {
            "event_type" : "Anniversary",
            "event_date" : "2015-03-24"
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("58736cfc7d43c305461cdba9"),
    "Name" : "Pole",
    "pb_event" : [ 
        {
            "event_type" …
Run Code Online (Sandbox Code Playgroud)

mongodb mongodb-query aggregation-framework

5
推荐指数
1
解决办法
3427
查看次数

在 ElasticSearch 中从数组中删除元素/对象,然后匹配查询

我有以下类型的文件。

{
"_index": "example1",
"_type": "doc",
"_id": "8036",
"_score": 1,
"_source": {
  "user": "kimchy8036",
  "postDate": "2009-11-15T13:12:00",
  "locations": [
    [
      72.79887719999999,
      21.193036000000003
    ],
    [
      -1.8262150000000001,
      51.178881999999994
    ]
  ]
  }
} 
Run Code Online (Sandbox Code Playgroud)

下面是映射:

   {
        "example1": {
        "mappings": {
        "doc": {
        "properties": {
        "locations": {
        "type": "geo_point"
        },
        "postDate": {
        "type": "date"
        },
        "status": {
        "type": "long"
        },
        "user": {
        "type": "text",
        "fields": {
        "keyword": {
        "type": "keyword",
        "ignore_above": 256
        }
        }
        }
        }
        }
        }
        }
            }
Run Code Online (Sandbox Code Playgroud)

我可以使用以下查询添加多个位置。

POST /example1/_update_by_query
    {
    "query": { …
Run Code Online (Sandbox Code Playgroud)

arrays geolocation match elasticsearch

5
推荐指数
2
解决办法
6812
查看次数