作者: shing1630 時間: 2016-6-24 18:00 標題: 有無人識 mongoDB find既用法?
mongoDB find個到
有無得check個input value 係null 就拎曬佢出黎
如果個input value 有野 就搵果個data出黎?
作者: hihihi123hk 時間: 2016-6-25 00:22
mongoDB find個到
有無得check個input value 係null 就拎曬佢出黎
如果個input value 有野 就搵果個data出 ...
shing1630 發表於 2016-6-24 18:00
find({ abc : { $exists : fasle }})
via HKEPC Ionic Reader v1.3.3 - iPhone
作者: kolaimui 時間: 2016-6-25 00:29
本帖最後由 kolaimui 於 2016-6-25 00:43 編輯
need clarify... check個input value 係null 定check 個input field係null? 如果想搵曬所有唔包含某個field既records, 用 { field: { $exists: false } } . 注意番如果個field 存在但係個value 係null 都要用 { field: { $exists: true} } 先搵到.
https://docs.mongodb.com/manual/reference/operator/query/exists/
v===========原本既理解============v
響application level做null checking, 然後分做兩個query?
if( peopleName === null ) {
People.find({});
} else {
People.find({ name: peopleName} );
}
作者: shing1630 時間: 2016-6-27 09:07
我諗都要分唔同既query 去做
唔該曬各位
作者: shing1630 時間: 2016-6-27 22:24
help again!!!
假設有以下 json
{ "_id": "apples", "qty": { "cat": "A", "cat":"B" } }
{ "_id": "bananas", "qty": { "cat": "B", "cat":"C" } }
{ "_id": "oranges", "qty": { "cat": "A", "cat":"E" } }
{ "_id": "avocados", "qty": { "cat": "D", "cat":"F" } }
咁我就咁寫個find
db.collection.find({
"qty" : { cat:"A", cat:"B"
}});
但係個result 點解會return
apples, bananas, oranges
其實我只想要apples

作者: kolaimui 時間: 2016-6-27 22:44
本帖最後由 kolaimui 於 2016-6-27 23:00 編輯
you have two cat key in the json object... XD
http://stackoverflow.com/questio ... s-need-to-be-unique
I would try to modify them to array.
/* 1 */
{
"_id" : "apples",
"cats" : [
"A",
"B"
]
}
/* 2 */
{
"_id" : "bananas",
"cats" : [
"B",
"C"
]
}
/* 3 */
{
"_id" : "oranges",
"cats" : [
"A",
"E"
]
}
/* 4 */
{
"_id" : "avocados",
"cats" : [
"D",
"F"
]
}
//Then, use $all to do the query on the array
//https://docs.mongodb.com/v3.0/reference/operator/query/all/
db.getCollection('fruits').find({ cats: { $all: [ "A" , "B" ] } })
作者: shing1630 時間: 2016-6-28 12:18
you have two cat key in the json object... XD
I would try to modify them to array.
/* 1 */
{
...
kolaimui 發表於 2016-6-27 22:44
係wo!
thanks

