【问题标题】:How to do a findAll using mongoosejs?如何使用 mongoosejs 进行 findAll?
【发布时间】:2012-11-20 04:06:33
【问题描述】:

这是我的 LinkSchema:

var LinkSchema = new Schema({

  user: ObjectId,

  text: {
      type: String,
      validate: [required,"Text is required"],
      index: {unique: true}
    },
    body: {
        type: String,
        validate: [required, 'Body is required'],
        index: { unique: true }
    },
    createdAt: {
        type: Date,
        'default': Date.now
    }
});

这是我的 getLink:

LinkSchema.statics.getLink = function(apiKey,fn){

    var query = link.find('link.user.apiKey': apiKey);

    query.exec(function (err, links) {
      if (err) return handleError(err);
        res.send(items);

    });
}

错误:

Unexpected Token':'  -> var query = link.find('link.user.apiKey': apiKey);

我想我在做 mongoosejs 的 find() 错误。我该如何解决这个问题?

【问题讨论】:

  • 我在您的架构中的任何地方都看不到 link.user.apiKey,但您正在对其进行过滤。你能详细说明你想做的事情吗?

标签: node.js mongodb mongoose


【解决方案1】:

你可以这样做:

var Link = db.model('Link', LinkSchema);
Link.find({}, function(err, results) {
    // res.send(results); for example.
});

find 函数的第一个参数是查询。例如,如果你想搜索所有body等于blablabla的链接:

Link.find({body: 'blablabla'}, function(err, results) {
    // res.send(results); for example.
});

【讨论】:

  • 但这不会让我检索到具有特定 apiKey 的所有链接。
猜你喜欢
  • 2013-07-09
  • 2011-11-04
  • 2013-08-26
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
相关资源
最近更新 更多