简体   繁体   中英

Using imported docs from MongoDB in DerbyJS

I have a MongoDB collection with data that was not saved through my Derby app. I want to query against that and pull it into my Derby app.

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. Here's an example of the JSON that the model.get() returns: https://gist.github.com/0a5426d2b28a940e8803

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. I built a hack to get around it for now, but it's not as smooth as I wish it would be. 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 ?

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. 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

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"}]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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