Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将SQLite查询转换为MongoDB查询_Javascript_Database_Mongodb_Mongoose_Aggregation Framework - Fatal编程技术网

Javascript 如何将SQLite查询转换为MongoDB查询

Javascript 如何将SQLite查询转换为MongoDB查询,javascript,database,mongodb,mongoose,aggregation-framework,Javascript,Database,Mongodb,Mongoose,Aggregation Framework,如何使用聚合框架将此SQLite查询转换为MongoDB查询 SQLite查询: " FROM car WHERE (@make is NULL OR @make = make) AND (@model is NULL OR @model = model) AND (@minPrice is NULL OR @minPrice <= price) AND (@maxPrice is NULL OR @maxPrice >= price) &q

如何使用聚合框架将此SQLite查询转换为MongoDB查询

SQLite查询:

"
FROM car
    WHERE (@make is NULL OR @make = make)
    AND (@model is NULL OR @model = model)
    AND (@minPrice is NULL OR @minPrice <= price)
    AND (@maxPrice is NULL OR @maxPrice >= price)
"
但当不匹配“make”时,它不会返回任何结果

[
  {
    $match: {
      $and: [
        { $or: [{ make: null }, { make: "asds" }] },
        { $or: [{ model: null }, { model: "320" }] },
        { $or: [{ price: null }, { price: { $gte: 1000 } }] },
        { $or: [{ price: null }, { price: { $lte: 80000 } }] },
      ],
    },
  },
  { $project: { _id: 0, make: 1, model: 1, price: 1 } },
];

我通过将
{field:null}
替换为
{field:{$exists:| |}

详细显示mongodb

所以我有这个“汽车”系列

如果我搜索

  • make=‘宝马’
  • 模型='asd'(我们的数据库集合中未提供值)
  • 最低价格=1000
  • maxPrice=40000
  • 将返回此结果

    没有此具有“价格=50999”的文档

    {make: 'BMW', model: '320', price: 50999}
    
    这意味着“价格”条件也在起作用

    让我们编写此聚合的代码

    我有一个管道,它有两个阶段“$match”和“$project”

    const pipeline = [
      {
        $match: {
          $and: [
            { $or: [{ make: { $exists: false } }, { make: "BMW" }] },
            { $or: [{ model: { $exists: true } }, { model: "asd" }] },
            { $or: [{ price: { $exists: false } }, { price: { $gte: 1000 } }] },
            { $or: [{ price: { $exists: false } }, { price: { $lte: 50000 } }] },
          ],
        },
      },
      { $project: { _id: 0, make: 1, model: 1, price: 1 } },
    ];
    
    在这种“$或”情况下

    将检查“make”字段是否存在。

    如果不存在($exists:false),将继续执行“$或”运算符中的下一个条件;如果为true,将继续执行“$和”运算符中的下一个“$或”条件,


    如果存在($exists:true)将只继续到“$and”运算符中的下一个“$或”条件,依此类推。

    我通过将
    {field:null}
    替换为
    {field:{$exists:|}

    详细显示mongodb

    所以我有这个“汽车”系列

    如果我搜索

  • make=‘宝马’
  • 模型='asd'(我们的数据库集合中未提供值)
  • 最低价格=1000
  • maxPrice=40000
  • 将返回此结果

    没有此具有“价格=50999”的文档

    {make: 'BMW', model: '320', price: 50999}
    
    这意味着“价格”条件也在起作用

    让我们编写此聚合的代码

    我有一个管道,它有两个阶段“$match”和“$project”

    const pipeline = [
      {
        $match: {
          $and: [
            { $or: [{ make: { $exists: false } }, { make: "BMW" }] },
            { $or: [{ model: { $exists: true } }, { model: "asd" }] },
            { $or: [{ price: { $exists: false } }, { price: { $gte: 1000 } }] },
            { $or: [{ price: { $exists: false } }, { price: { $lte: 50000 } }] },
          ],
        },
      },
      { $project: { _id: 0, make: 1, model: 1, price: 1 } },
    ];
    
    在这种“$或”情况下

    将检查“make”字段是否存在。

    如果不存在($exists:false),将继续执行“$或”运算符中的下一个条件;如果为true,将继续执行“$和”运算符中的下一个“$或”条件,


    如果存在($exists:true),则只会继续到“$and”运算符中的下一个“$或”条件,依此类推。

    以下是将SQL转换为聚合查询的指导原则:。非常感谢我尝试了很多转换,每件事都按预期进行,但当“make”时not match它返回nothing有一些将SQL转换为聚合查询的指导原则:。非常感谢我尝试转换,一切都按预期进行,但当“make”不匹配时,它将返回nothing
    const pipeline = [
      {
        $match: {
          $and: [
            { $or: [{ make: { $exists: false } }, { make: "BMW" }] },
            { $or: [{ model: { $exists: true } }, { model: "asd" }] },
            { $or: [{ price: { $exists: false } }, { price: { $gte: 1000 } }] },
            { $or: [{ price: { $exists: false } }, { price: { $lte: 50000 } }] },
          ],
        },
      },
      { $project: { _id: 0, make: 1, model: 1, price: 1 } },
    ];
    
    { $or: [{ make: { $exists: false } }, { make: "BMW" }] }