在MongoDB上查找和Upsert

时间:2017-06-27 05:46:54

标签: mongodb mongodb-query

我有一个mongodb文档,其架构如下

{
    "userid":"abc123",
    "applications": [
        {
            "app_id":"12345",
            "name":"application_1",
            "settings": [
                {
                    "settingid":"1",
                    "settingkey":"refreshTime",
                    "settingValue":"20mins",
                    "isActive":"yes",
                    "updatedOn":"2017-06-22"
                },
                {
                    "settingid":"2",
                    "settingkey":"refreshTime",
                    "settingValue":"10mins",
                    "isActive":"no",
                    "updatedOn":"2017-06-22"
                }
            ]
        },
        {
            "app_id":"8999",
            "name":"application_12",
            "settings": [
                {
                    "settingid":"1",
                    "settingkey":"refreshTime",
                    "settingValue":"20mins",
                    "isActive":"yes",
                    "updatedOn":"2017-06-22"
                }
            ]
        }
    ],
    "userid":"abc",
    "applications": [
        {
            "app_id":"12345",
            "name":"application_1",
            "settings": [
                {
                    "settingid":"1",
                    "settingkey":"refreshTime",
                    "settingValue":"20mins",
                    "isActive":"yes",
                    "updatedOn":"2017-06-22"
                },
                {
                    "settingid":"2",
                    "settingkey":"refreshTime",
                    "settingValue":"10mins",
                    "isActive":"no",
                    "updatedOn":"2017-06-22"
                }
            ]
        },
        {
            "app_id":"8999",
            "name":"application_12",
            "settings": [
                {
                    "settingid":"1",
                    "settingkey":"refreshTime",
                    "settingValue":"20mins",
                    "isActive":"yes",
                    "updatedOn":"2017-06-22"
                }
            ]
        }
    ]
}

查找用户是否存在。 如果用户存在,则查找是否存在应用程序 如果应用程序存在找到该设置。 如果设置存在更新设置或插入新设置。

db.usersettings.find({"userid":"abc123"})

db.usersettings.find({applications.name:"appname"})

db.usersettings.find({applications.settings.settingKey:"settingkeyname"})

如何通过检查存在并在数据库上执行插入和upsert来在单个查询中执行所有这些查询。

0 个答案:

没有答案