【问题标题】:How to store/retrieve into mongodb using mongoose ORM如何使用 mongoose ORM 存储/检索到 mongodb
【发布时间】:2011-10-29 13:00:32
【问题描述】:

我是 mongodb 和 mongoose orm 的新手。我写了一个示例咖啡脚本来将数据存储到 mongodb 中,但是没有创建数据库, 这是我的代码:

mongoose = require('mongoose')

db = mongoose.connect('mongodb://localhost/test')

people = [{
    bio: 'hello1'
    first_name: 'jay'
    last_name: 'roger'
  },{
    bio: 'hello2'
    first_name: 'jay'
    last_name: 'roger'
  }]

artist_schema = new mongoose.Schema
     bio: String
     first_name: String
     last_name: String

artist_model = mongoose.model "artist", artist_schema

artist_doc = new mongoose.Collection 'artists', db

for person in people
    artist = new artist_model person
    artist_doc.insert artist

执行上述脚本后,mongodb中并没有创建db。

我错过了什么吗?

问候, 通用汽车

【问题讨论】:

  • 得到了存储数据的解决方案,正确的代码是:for person in people: artist = new artist_model person; artist.save()

标签: mongodb coffeescript mongoose


【解决方案1】:

我看到了您的评论,但想建议(对于可能会发现此问题的其他人)一种我认为更可取的理解方式来做到这一点。更改最后三行:

for person in people
    artist = new artist_model person
    artist_doc.insert artist

到:

artist_doc.insert new artist_model(person) for person in people

【讨论】:

    【解决方案2】:

    示例如何使用 Mongo 模型和 CoffeeScript http://alexeypetrushin.github.com/mongo-model/basics.html

    【讨论】:

      【解决方案3】:

      无需创建artist_doc

      只做artist.save

      【讨论】:

        猜你喜欢
        • 2017-10-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 2016-04-07
        • 2020-04-22
        • 2021-02-03
        • 1970-01-01
        相关资源
        最近更新 更多