简体   繁体   English

JavaScript中的键值对

[英]key value pair in javascript

Applying filters in MongoDb 在MongoDb中应用过滤器

I need to apply filters in mongoDb in an embedded document so how can I make a query like 我需要在嵌入式文档的mongoDb中应用过滤器,以便如何进行查询

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 情况1:我的查询应该像

 var query = {
    _id:userId,
    'match.Id':matchId,

    }

case 2 : 情况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 所以我的问题是如何在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 comes with square bracket but type of arr is object 尝试下面的代码获得了所需的输出,但带有方括号,但是arr的类型是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'}

Change [] to {} []更改为{}

 var obj = {}; 
 obj[ 'key3.abc' ] = "value3";       
 obj[ 'key2.abc' ] = "value3"; 

 console.log(obj) // { 'key3.abc': 'value3', 'key2.abc': 'value3'}

NB We can assign or access a JavaScript object by square ( [] ) notation when key contains special character eg space, dot etc. 注意当键包含special character例如空格,点等)时,我们可以通过方( [] )表示法分配或访问JavaScript对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM