MongoDB如何添加条件$ match条件

时间:2018-10-31 10:58:08

标签: mongodb mongoose aggregation-framework

我有以下模式

{
   f1: "test",
   f2: "something",
   type: "A",
   date: "2018-11-01T00:00:00.000Z",
   deleted: false
},
{
   "f1": "check",
   type: "B",
   deleted: false
}

现在我想要的是获取所有数据,如果是type = "A", then add an extra condition to my match query, suppose compare its date with current date。 我当前的查询是:

db.getCollection('myCollection').aggregate([
  {$match:{
        {"deleted":false},
        // I want to check if type is A then compare its date 
  }}
])

2 个答案:

答案 0 :(得分:1)

pi@picroft:~ $ bash ./update.sh Would you like to install Picroft on this machine? Choice [Y/N]: mkdir: cannot create directory ‘/etc/mycroft/’: File exists mkdir: cannot create directory ‘/home/pi/bin’: File exists --2018-10-31 10:57:28-- https://raw.githubusercontent.com/MycroftAI/enclosure-picroft/stretch/etc/mycroft/mycroft.conf Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.116.133 Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.116.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 285 [text/plain] Saving to: ‘mycroft.conf’ mycroft.conf 100%[===================>] 285 --.-KB/s in 0s Last-modified header missing -- time-stamps turned off. 2018-10-31 10:57:28 (3.00 MB/s) - ‘mycroft.conf’ saved [285/285] [Service] ExecStart= ExecStart=-/sbin/agetty --autologin pi --noclear %I 38400 linux tmpfs /ramdisk tmpfs rw,nodev,nosuid,size=20M 0 0 Installing 'git'... Reading package lists... Done Building dependency tree Reading state information... Done git is already the newest version (1:2.11.0-3+deb9u4). The following packages were automatically installed and are no longer required: erlang-base erlang-crypto erlang-syntax-tools fonts-lato icedtea-netx-common libatk-wrapper-java libatk-wrapper-java-jni libboost-thread1.62.0 libqt5opengl5 libqt5scintilla2-12v5 libqt5scintilla2-l10n libqt5x11extras5 libqwt-qt5-6 libruby2.3 libscsynth1 libsctp1 libyaml-0-2 rake ruby ruby-did-you-mean ruby-minitest ruby-net-telnet ruby-power-assert ruby-test-unit ruby2.3 rubygems-integration Use 'sudo apt autoremove' to remove them. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 2 not fully installed or removed. After this operation, 0 B of additional disk space will be used. Setting up ca-certificates-java (20170531+nmu1) ... Error: missing `server' JVM at `/usr/lib/jvm/java-8-openjdk-armhf/jre/lib/arm/server/libjvm.so'. Please install or use the JRE or JDK that contains these missing components. dpkg: error processing package ca-certificates-java (--configure): subprocess installed post-installation script returned error exit status 4 dpkg: dependency problems prevent configuration of openjdk-8-jre-headless:armhf: openjdk-8-jre-headless:armhf depends on ca-certificates-java; however: Package ca-certificates-java is not configured yet. dpkg: error processing package openjdk-8-jre-headless:armhf (--configure): dependency problems - leaving unconfigured Processing triggers for ca-certificates (20161130+nmu1+deb9u1) ... Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done. Running hooks in /etc/ca-certificates/update.d... Error: missing `server' JVM at `/usr/lib/jvm/java-8-openjdk-armhf/jre/lib/arm/server/libjvm.so'. Please install or use the JRE or JDK that contains these missing components. E: /etc/ca-certificates/update.d/jks-keystore exited with code 1. done. Errors were encountered while processing: ca-certificates-java openjdk-8-jre-headless:armhf E: Sub-process /usr/bin/dpkg returned an error code (1) Downloading 'mycroft-core'... fatal: destination path 'mycroft-core' already exists and is not an empty directory. Beginning building mycroft-core. This'll take a bit, take a break. Results will be in the ~/build.log Installing packages... Reading package lists... Building dependency tree... Reading state information... autoconf is already the newest version (2.69-10). automake is already the newest version (1:1.15-6). bison is already the newest version (2:3.0.4.dfsg-1). build-essential is already the newest version (12.3). curl is already the newest version (7.52.1-5+deb9u7). flac is already the newest version (1.3.2-1). git is already the newest version (1:2.11.0-3+deb9u4). jq is already the newest version (1.5+dfsg-1.3). libfann-dev is already the newest version (2.2.0+ds-3). libffi-dev is already the newest version (3.2.1-6). libglib2.0-dev is already the newest version (2.50.3-2). libicu-dev is already the newest version (57.1-6+deb9u2). libjpeg-dev is already the newest version (1:1.5.1-2). libssl-dev is already the newest version (1.1.0f-3+deb9u2). libtool is already the newest version (2.4.6-2). mpg123 is already the newest version (1.23.8-1). pkg-config is already the newest version (0.29-4). portaudio19-dev is already the newest version (19.6.0-1). python-gobject-2-dev is already the newest version (2.28.6-13). python-setuptools is already the newest version (33.1.1-1). python3 is already the newest version (3.5.3-1). python3-dev is already the newest version (3.5.3-1). screen is already the newest version (4.5.0-6). swig is already the newest version (3.0.10-1.1). The following packages were automatically installed and are no longer required: erlang-base erlang-crypto erlang-syntax-tools fonts-lato icedtea-netx-common libatk-wrapper-java libatk-wrapper-java-jni libboost-thread1.62.0 libqt5opengl5 libqt5scintilla2-12v5 libqt5scintilla2-l10n libqt5x11extras5 libqwt-qt5-6 libruby2.3 libscsynth1 libsctp1 libyaml-0-2 rake ruby ruby-did-you-mean ruby-minitest ruby-net-telnet ruby-power-assert ruby-test-unit ruby2.3 rubygems-integration Use 'sudo apt autoremove' to remove them. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 2 not fully installed or removed. After this operation, 0 B of additional disk space will be used. Setting up ca-certificates-java (20170531+nmu1) ... Error: missing `server' JVM at `/usr/lib/jvm/java-8-openjdk-armhf/jre/lib/arm/server/libjvm.so'. Please install or use the JRE or JDK that contains these missing components. dpkg: error processing package ca-certificates-java (--configure): subprocess installed post-installation script returned error exit status 4 dpkg: dependency problems prevent configuration of openjdk-8-jre-headless:armhf: openjdk-8-jre-headless:armhf depends on ca-certificates-java; however: Package ca-certificates-java is not configured yet. dpkg: error processing package openjdk-8-jre-headless:armhf (--configure): dependency problems - leaving unconfigured Processing triggers for ca-certificates (20161130+nmu1+deb9u1) ... Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done. Running hooks in /etc/ca-certificates/update.d... Error: missing `server' JVM at `/usr/lib/jvm/java-8-openjdk-armhf/jre/lib/arm/server/libjvm.so'. Please install or use the JRE or JDK that contains these missing components. E: /etc/ca-certificates/update.d/jks-keystore exited with code 1. done. Errors were encountered while processing: ca-certificates-java openjdk-8-jre-headless:armhf E: Sub-process /usr/bin/dpkg returned an error code (1) Build complete. Press any key to review the output before it is deleted. 条件下使用$match

$or

答案 1 :(得分:1)

您可以尝试使用dense_rank()并说“如果不是键入A ,则日期为x”:

$or