MongoDB-设置是否为空或空查询运算符

时间:2019-02-26 23:23:52

标签: mongodb

我想知道是否存在$setIfNullOrEmpty运算符。我目前正在使用这样的upsert:

const filter = {
    id: 123
}
const updates = {
    $set: {
        varx: valx
    },
    $inc: { vary: valy },
    $setOnInsert: z
};
const options = { upsert: true };
collection.updateOneWithOptions(filter, updates, options);

如果数据库中的值为空或空字符串,我还想选择$set。我理想的updates对象看起来像这样(是否存在这样的东西?):

const updates = {
        $set: {
            varx: valx
        },
        $setIfNullOrEmpty: {
            varxy: varxy
        }
        $inc: { vary: valy },
        $setOnInsert: z
    };

我知道我可以进行2个查询(其中1个用于获取我要查找的项目,检查该属性,另一个用于更新该项目),但是我正在寻找一种方法来进行1个查询。有办法吗?

2 个答案:

答案 0 :(得分:1)

  

这可以使用$ cond运算符

实现

尝试以下命令

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CreateActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblTitle"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:text="Title:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblTitleEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your title here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblVenue"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:text="Venue:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblVenueEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your venue here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

根据docs

  

$ inc运算符将字段增加指定值。

     

如果该字段不存在,则$ inc创建该字段并设置该字段   到指定值。

因此您当前的查询有效。如果找到的文档没有字段vary,则会创建该文档并将其设置为您指定的值。

相关问题