第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 SQLite 查詢轉換為 MongoDB 查詢

如何將 SQLite 查詢轉換為 MongoDB 查詢

海綿寶寶撒 2023-06-29 15:51:25
如何使用聚合框架將此 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)"我嘗試轉換為 mongodb 聚合 Famework,一切都按預期運行[  {    $match: {      $and: [        { $or: [{ make: null }, { make: "BMW" }] },        { $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 } },];但是當不匹配“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 } },];
查看完整描述

1 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

我通過替換解決了這個{field: null}問題{field: {$exists: <false> || <true> } }

詳細顯示 mongodb?$exists 運算符

所以我有這個“汽車”收藏

[
?{make:?'Audi',?model:?'A2',?price:?13999},
?{make:?'BMW',?model:?'116',?price:?12999},
?{make:?'BMW',?model:?'X1',?price:?18999},
?{make:?'BMW',?model:?'320',?price:?24000},
?{make:?'BMW',?model:?'320',?price:?50999},
?{make:?'Ford',?model:?'Fiesta',?price:?14999},
?{make:?'Mazda',?model:?'6',?price:?13999},
?{make:?'Merces-Benz',?model:?'200',?price:?38999},
?{make:?'Mazda',?model:?'6',?price:?16999},
?{make:?'Mazda',?model:?'e250',?price:?11999},
]

如果我搜索

  1. 品牌 = '寶馬'

  2. model = 'asd'(我們的數據庫集合中未提供值)

  3. 最低價格 = 1000

  4. 最高價格 = 40000

將返回這個結果

?{make:?'BMW',?model:?'116',?price:?12999},
?{make:?'BMW',?model:?'X1',?price:?18999},
?{make:?'BMW',?model:?'320',?price:?24000},

沒有此文件,其中包含“價格= 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?}?},
];

在這個“$or”條件下

{?$or:?[{?make:?{?$exists:?false?}?},?{?make:?"BMW"?}]?}

將檢查“make”字段是否存在。

如果不存在 ($exists: false) 將繼續(xù)到“$or”運算符中的下一個條件,如果為 true 將繼續(xù)到“$and”運算符中的下一個“$or”條件,

if exit ($exists: true) 只會繼續(xù)到“$and”運算符中的下一個“$or”條件,依此類推。


查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號