admin管理员组文章数量:1336293
Applying filters in MongoDb
I need to apply filters in mongoDb in an embedded document so how can I make a query like
Example:
var query = {
_id:userId,
'match.Id':matchId,
'match.userId':userId1
}
now I want to apply filters lets suppose
case 1: my query should be like
var query = {
_id:userId,
'match.Id':matchId,
}
case 2 :
var query = {
_id:userId,
'match.userId':userId1
}
there can be many cases like this
So my question is how can I make this query object in node.js/javascript
My work : I can create multiple key in an object but creating key as below doesn't works
var query={}
query._id:userId // works
query.'match.userId':matchId // error
query.match.userId:matchId //error
tried below code got desired output but it es with square bracket but type of arr is object
var arr = [];
arr[ 'key3.abc' ] = "value3";
arr[ 'key2.abc' ] = "value3";
console.log(arr)//[ 'key3.abc': 'value3', 'key2.abc': 'value3' ]
desired output:
{'key3.abc': 'value3', 'key2.abc': 'value3'}
Applying filters in MongoDb
I need to apply filters in mongoDb in an embedded document so how can I make a query like
Example:
var query = {
_id:userId,
'match.Id':matchId,
'match.userId':userId1
}
now I want to apply filters lets suppose
case 1: my query should be like
var query = {
_id:userId,
'match.Id':matchId,
}
case 2 :
var query = {
_id:userId,
'match.userId':userId1
}
there can be many cases like this
So my question is how can I make this query object in node.js/javascript
My work : I can create multiple key in an object but creating key as below doesn't works
var query={}
query._id:userId // works
query.'match.userId':matchId // error
query.match.userId:matchId //error
tried below code got desired output but it es with square bracket but type of arr is object
var arr = [];
arr[ 'key3.abc' ] = "value3";
arr[ 'key2.abc' ] = "value3";
console.log(arr)//[ 'key3.abc': 'value3', 'key2.abc': 'value3' ]
desired output:
{'key3.abc': 'value3', 'key2.abc': 'value3'}
Share
asked Feb 2, 2017 at 2:41
Shumi GuptaShumi Gupta
1,5252 gold badges19 silver badges28 bronze badges
1
- Probably a duplicate of How can I add a key/value pair to a JavaScript object?. – RobG Commented Feb 2, 2017 at 3:02
1 Answer
Reset to default 5Change []
to {}
var obj = {};
obj[ 'key3.abc' ] = "value3";
obj[ 'key2.abc' ] = "value3";
console.log(obj) // { 'key3.abc': 'value3', 'key2.abc': 'value3'}
N.B. We can assign or access a JavaScript object by square ([]
) notation when key contains special character
e.g. space, dot etc.
本文标签: nodejskey value pair in javascriptStack Overflow
版权声明:本文标题:node.js - key value pair in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406272a2468883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论