2016-08-11 48 views
0

とのNode.jsでクエリを構築することはできません私:

"Error :MongoError: Can't canonicalize query: BadValue $and/$or/$nor must be a nonempty array"

または私はクエリとして$または{}

"Error :MongoError: Can't canonicalize query: BadValue $or needs an array"

感謝初期化します。を!例えば

var user = { 
 
     name:"Toby", 
 
     gender:"Female", 
 
     contact:[ 
 
     {email:"[email protected]"}, 
 
     {email:"[email protected]"}, 
 
     {email:"[email protected]"}]}; 
 
var query = {}; 
 
var q = ['[email protected]','[email protected]']; 
 
query.name = user.name; 
 
if(user.contact!=null){ 
 
    query.$or = []; 
 
    query.$or['contact.0.email'] = q[0]; 
 
    query.$or['contact.1.email'] = q[1]; 
 
}

+0

ようなSQLコマンド:あなたのユーザからのemail='[email protected]「OR email='[email protected]」*を選択し、名前= 『トビー』 – yycctt

+0

感謝を回答!そしてもう一つの解決策も見つかった:query1.contact = {$ elemMatch:{email:{}}}; query1.contact。$ elemMatch.email。$ in = q; – yycctt

答えて

2

$または可能な一致条件の配列である必要があります。あなたは何ができる:

var user = { 
     name:"Toby", 
     gender:"Female", 
     contact:[ 
     {email:"[email protected]"}, 
     {email:"[email protected]"}, 
     {email:"[email protected]"}]}; 
var query = {}; 
var q = ['[email protected]','[email protected]']; 
query.name = user.name; 
if(user.contact!=null){ 
    query.$or = []; 
    query.$or.push({'contact.email': q[0]}); 
    query.$or.push({'contact.email': q[1]}); 
} 
関連する問題

 関連する問題