我怎样才能让MongoDB评估一些JavaScript以获得一个领域的价值? [英] How can I ask MongoDB to evaluate some JavaScript in order to obtain value for a field?

查看:83
本文介绍了我怎样才能让MongoDB评估一些JavaScript以获得一个领域的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让MongoDB动态地为我插入的文档的某个字段赋值。例如:MongoDB服务器的当前时间就像NOW()在MySQL中所做的那样。



我试过这个:

  c:= mongoSession.DB(myapp)。C(instances)
rand.Seed(time.Now()。UnixNano())
err:= c.Insert(
struct {Serial,Priority,Url,LastSeen interface {}} {
Url:getInformedHost()+:+ getRunningPortString(),
Priority :rand.Int(),
LastSeen:mongoNow()}

checkError(err,无法在MongoDB服务器上注册,3)



我有这个帮助函数:

pre $ code > func mongoNow()bson.JavaScript {
return bson.JavaScript {Code:
(new Date())。ISODate('YYYY-MM-DD hh:mm:ss')}
}

LastSeen字段以脚本形式存储而不是评估:

  [_ id] => MongoId对象(
[$ id] => 502d6f984eaead30a134fa10

[priority] => 1694546828
[url] => 127.0.0.1:8080
[lastseen] => MongoCode对象(
[code] =>(new Date())。ISODate('YYYY-MM-DD hh:mm:ss')
[scope] => Array(


我怎样才能得到一些javascript而不是插入的?

解决方案

请参阅MongoDB文档中的以下URL: http://www.mongodb.org/display/DOCS/Server -batch +执行#server-sideCodeExecution-Storingfunctionsserverside


有一个特殊的系统集合称为 system.js 可以存储JavaScript函数以供重用。

请注意,支持和服务器端代码的性能(相当于存储过程)仍然有点差(详细信息请参考链接)。



编辑:



使用 mgo驱动程序从Go调用存储过程>使用 mgo.Database 类型的 Run()方法( direct link ),然后用Javascript代码发出 eval 命令作为参数执行服务器端。例如:

  db.Run(bson.M {eval:myStoredFunction();})

代码未经测试

无法在MongoDB insert 语句中评估代码。


I want to let MongoDB dynamically assign a value to one of the fields of the document I'm inserting. For example: the current time from MongoDB server just like NOW() would do in MySQL.

I tried this:

c := mongoSession.DB("myapp").C("instances")
rand.Seed(time.Now().UnixNano())
err := c.Insert(
   struct{Serial, Priority, Url, LastSeen interface{}}{ 
      Url: getInformedHost() + ":" + getRunningPortString(), 
      Priority: rand.Int(), 
      LastSeen: mongoNow() }
)
checkError(err, "Could not register on MongoDB server.", 3)

I have this helper function:

func mongoNow() bson.JavaScript {
    return bson.JavaScript{Code: 
         "(new Date()).ISODate('YYYY-MM-DD hh:mm:ss')"}
}

the LastSeen field gets stored as a script instead of evaluated:

[_id] => MongoId Object (
    [$id] => 502d6f984eaead30a134fa10
)
[priority] => 1694546828
[url] => 127.0.0.1:8080
[lastseen] => MongoCode Object (
    [code] => (new Date()).ISODate('YYYY-MM-DD hh:mm:ss')
    [scope] => Array (
    )
)

How can I get some javascript evaluated instead of inserted?

解决方案

See the following URL on the MongoDB documentation:

http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-Storingfunctionsserverside

There is a special system collection called system.js that can store JavaScript functions to be reused.

Note though, that the support and performance of server-sided code (equivalent to stored procedures) is still a little poor (details in link).

Edit:

To call a stored procedure from Go using the mgo driver use the mgo.Database type's Run() method (direct link) and issue an eval command with the Javascript code to be executed server-side as argument. Something like:

db.Run(bson.M{"eval": "myStoredFunction();"})

code untested

It is not possible to have code evaluated in a MongoDB insert statement.

这篇关于我怎样才能让MongoDB评估一些JavaScript以获得一个领域的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆