MongoDB - 拒绝对$ lookup记录中的值进行$ lookup的记录

时间:2017-04-17 09:47:08

标签: mongodb mongodb-query aggregation-framework

我想基于duedates获取所有发票 - 并根据预订表中的状态。

我想要发票预订到哪里.status< 5

我不希望发票在booking.status> = 5

这是我的聚合:

db.getCollection('invoice').aggregate([
    {
        $match: {
            dueDate: {$gte: 1483596800},
            dueDate: {$lte: 1583596800}
        }
    },
    {
        $lookup: {
            from: "booking",
            localField: "bookingId",
            foreignField: "_id",    
            as: "booking"       
        }   
    }
])

以下是表格......

table invoice
{
    "_id" : "IKUU",
    "bookingId" : "AAAAA",
    "dueDate" : 1489470468,
    "invoiceLines" : [ 
        {
            "lineText" : "Rent Price",
            "amountPcs" : "7 x 2071",
            "amountTotal" : 14497
        }, 
        {
            "lineText" : "Discount",
            "amountPcs" : "",
            "amountTotal" : -347
        } 
    ]
}
{
    "_id" : "1NYRO",
    "bookingId" : "BBBBB",
    "dueDate" : 1489471351,
    "invoiceLines" : [ 
        {
            "lineText" : "Reservation / Booking fee",
            "amountPcs" : "1 x 2000",
            "amountTotal" : 2000
        }
    ]
}


table booking
{
    "_id" : "AAAAA",
    "checkin" : 1449756800,
    "price" : 5000,
    "status" : 1
}
{
    "_id" : "BBBBB",
    "checkin" : 1449756800,
    "price" : 6000,
    "status" : 5
}

我尝试了一些$ match {booking.status:{$ lt:5}},但我无法让它发挥作用。

结果应该是" bookingId" :" AAAAA"。

1 个答案:

答案 0 :(得分:1)

您需要在$ lookup之后检查另一个匹配的预订状态

package ant;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;


public class NewTestNG {
public WebDriver driver;

@BeforeMethod
public void LAunchbrowser() {
  driver = new FirefoxDriver();
  driver.get("https://www.google.co.in/");
  //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

}
  @Test
  public void main() {
  Actions action = new Actions(driver);
  WebDriverWait wait = new WebDriverWait (driver, 20);
  WebElement w= 
   wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
  [@id='gs_htif0']")));
   WebElement a=   driver.findElement(By.xpath(".//*[@id='gs_htif0']"));    
   action.moveToElement(a).click().sendKeys("Shirt").build().perform(); 
  driver.findElement(By.xpath("//div[@value='Search']")).click();


 }}