简体   繁体   English

在DerbyJS中使用从MongoDB导入的文档

[英]Using imported docs from MongoDB in DerbyJS

I have a MongoDB collection with data that was not saved through my Derby app. 我有一个MongoDB集合,其中包含未通过我的Derby应用程序保存的数据。 I want to query against that and pull it into my Derby app. 我想对此进行查询,并将其放入我的Derby应用程序中。

So, I figured that out and here's the code to do it: 因此,我知道了,这是执行此操作的代码:

get '/:year', (page, model, params) ->
  query = model.query 'years', 
    where:
      year: (parseInt params.year, 10)
    limit: 1
  model.subscribe query, (err, year) ->
    if err
      console.log err

    page.render
      y: year.get(),
      year: params.year

The only problem is that the documents don't have a derby id, so it sets the MongoDB objectid (_id) to the derby id. 唯一的问题是文档没有derby id,因此它将MongoDB对象id(_id)设置为derby id。 Here's an example of the JSON that the model.get() returns: https://gist.github.com/0a5426d2b28a940e8803 这是model.get()返回的JSON的示例: https : model.get()

I have no way of knowing what the objectid is before the query. 我无法知道查询之前的对象标识是什么。 I would love if the most recently returned id ( currID ) was in the object's top level so I could just query that and set a model reference. 我希望最近返回的id( currID )是否在对象的顶层,因此我可以对其进行查询并设置模型引用。 I built a hack to get around it for now, but it's not as smooth as I wish it would be. 我现在建立了一个hack来解决它,但是它并没有我希望的那么顺利。 The code is below: 代码如下:

getId = (obj, year) ->
  for x of obj
    return obj[x].id if obj[x].year is year
  -1

Also, is there any way to update templates without destroying some templates already on the screen? 另外,有没有办法在不破坏屏幕上已有模板的情况下更新模板? Instead of doing a full page.render just doing a template.render ? 而不是做一个完整的page.render而是做一个template.render

Thanks 谢谢

Racer查询刚刚在0.3.11中进行了更新,并在以下自述文件中进行了描述: https : //github.com/codeparty/racer/blob/master/lib/descriptor/query/README.md

NOTE: This is no longer needed. 注意:不再需要。 The query system has been updated. 查询系统已更新。 Please see Nate's answer. 请参阅内特的答案。

I spoke with the developer of DerbyJS (nateps) and he told me that they are working on improving the query system. 我与DerbyJS(nateps)的开发人员进行了交谈,他告诉我他们正在致力于改进查询系统。 It's still a work in progress. 这项工作仍在进行中。

For now, I've built a hack which will take an object to test against and an object of parameters 现在,我已经建立了一个hack,它将使用一个要测试的对象和一个参数对象

getId = (obj, params) ->
  ids = []
  for x of obj
    i = 0
    for y of params
      if obj[x][y] isnt params[y]
        break
      else if (++i is params.length)
        ids.push
          id: obj[x].id
  return ids

For example, you can input an object like such: 例如,您可以输入如下对象:

var obj = {
  "lskjfalksj23423": {
    id: "lskjfalksj23423",
    name: "random day",
    year: 2012,
    month: 12,
    day: 1
  },
  "aklsdjflkasdfd": {
    id: "aklsdjflkasdfd",
    name: "random day 2",
    year: 2012,
    month: 8,
    day: 1
  }
}

and then an object to test against (MAKE SURE TO ADD A LENGTH KEY) 然后是要测试的对象(请务必添加长度键)

var params = {
  month: 12,
  day: 1,
  length: 2
}

and it will return an array of the matching ids: [{id: "lskjfalksj23423"}] 并将返回匹配ID的数组: [{id: "lskjfalksj23423"}]

Note: I haven't tested the example, so let me know if you're having problems with it. 注意:我尚未测试示例,所以如果您有问题,请告诉我。 This should be obsolete in a few months anyway when the query system is built out further. 无论如何,在进一步构建查询系统后的几个月内,这应该已经过时了。

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

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