简体   繁体   English

使用Javascript从MongoDB返回中提取值。

[英]Extracting a value from a MongoDB return, using Javascript.

Extracting an integer from the return of a MongoDB query, in Javascript. 使用Javascript从MongoDB查询的返回值中提取一个整数。

I'm planning to create some server-side processed queries for my MongoDB setup, using stored procedures in javascript. 我打算使用javascript中的存储过程为MongoDB设置创建一些服务器端处理的查询。 As Proof-of-concept, I'm trying to walk through the steps of extracting values from my existing test database in the Mongo shell, making them accessible for processing in javascript. 作为概念验证,我试图逐步完成从Mongo Shell中现有的测试数据库中提取值的步骤,使其可在javascript中进行处理。

My database consists of entries which includes a domain name, and the number of hits it has seen: 我的数据库由条目组成,该条目包括一个域名以及它看到的命中数:

{"dom":"domain1.org", "hits": 38}
{"dom":"domain2.com", "hits": 12}

For Proof of concept, I want to write a function 'addDomainHits' which will do the following: 对于概念验证,我想编写一个函数“ addDomainHits”,它将执行以下操作:

db.eval('addDomainHits("domain1.org","domain2.com")');
50

[Note 1: I'm perfectly aware that I could use MongoDB aggregations to perform this particular function, but that isn't the answer; [注1:我完全意识到我可以使用MongoDB聚合来执行此特定功能,但这不是解决方案; this is just a Proof of concept; 这只是概念证明; I actually want to do a lot more inside the functions I actually write.] 其实我是想我是不是真的写的函数里面多了很多 。]

[Note 2: Yes, this is horribly insecure, and open to code injection. [注2:是的,这非常不安全,可以进行代码注入。 I'll deal with that later. 我待会再处理。 Again, this is just Proof of Concept.] 同样,这只是概念证明。]

So, I tried to get the integer value into a variable in the MongoDB Shell, and that's where I run into problems. 因此,我尝试将整数值放入MongoDB Shell中的变量中,这就是我遇到的问题。

$ mongo
MongoDB shell version: 2.6.11
connecting to: test

> use test_database
switched to db test_database

> var r=db.test_collection.find({"dom":"domain1.org"},{hits:true,_id:false})
> r
{ "hits" : 38 }

Now, I want to get the '38' into simple integer variable, so I can do processing similar to: 现在,我想将'38'转换为简单的整数变量,因此我可以执行类似的处理:

> a=2
2
> b=3
3
> a+b
5

but, no joy: 但是,没有喜悦:

> r=db.test_collection.find({"dom":"domain1.org"},{hits:true,_id:false})
> r
{ "hits" : 38 }

> var r=db.test_collection.find({"dom":"domain1.org"},{hits:true,_id:false})
> r.hits
>

Notice that no value got returned 请注意,没有返回任何值

slight variant: 轻微变化:

> var r=db.test_collection.find({"dom":"domain1.org"},{hits:true,_id:false})
> var h=r.hits
> h
>

OR 要么

> var r=db.test_collection.find({dom:"seagoedd.org"},{hits:true,_id:false})
> var h=r['hits']
> h
>

So, in this example, how can I get a simple '38' into a variable? 因此,在此示例中,如何将简单的“ 38”放入变量中?

If you have one record per domain , use the findOne method, 如果每个domain有一条记录,请使用findOne方法,

var hits = db.test_collection.findOne({dom:"seagoedd.org"},
                                   {hits:true,_id:false})["hits"];

This works because this method returns a single document. 这行得通,因为此方法返回单个文档。 whereas find returns a cursor to the result list. findcursor返回到结果列表。

Note : if no records match, the findOne method would return you null . 注意 :如果没有记录匹配,则findOne方法将返回null in such cases, the below would make more sense: 在这种情况下,以下内容会更有意义:

var record = db.test_collection.findOne({dom:"seagoedd.org"},
                                   {hits:true,_id:false})
var hits = record?record["hits"]:0;

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

相关问题 使用正则表达式和Javascript从字符串中提取值 - Extracting value from string using regex and Javascript 提取 JavaScript function 返回值使用 Python ZC49DFB55F06BB406E2C5ZD8F6 - Extracting JavaScript function return value using Python Selenium 使用javascript以新值提交表单。 (MVC) - submit form with a new value using javascript. (MVC) 检测行并使用javascript获取td值。 (动态桌) - Detect the row and get the td value using javascript. (Dynamic table) 使用 mongoose 从 mongodb(非数组)返回值 - Return value from mongodb (not array) using mongoose 获取从XML到JavaScript的变量值。 纯粹的方式 - Get variable value from XML to JavaScript. Pure way 如何在从javascript中的json对象提取字符串时保留转义序列。 (字符串实际上是正则表达式模式) - How to retain escape sequences while extracting strings from json objects in javascript. (Strings are actually regex patterns) 如何使用javascript从网站提取数据。 - How do I extract data from a website using javascript. 使用Javascript解析JSON。 来自Laravel的JSON - Parsing JSON using Javascript. JSON from Laravel 使用JavaScript从表单中获取数据并将其放在表格行中。 - Taking data from the form and put it in the table row by using JavaScript.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM