【问题标题】:How to solve 'TypeError: db.find(...).toArray is not a function' in mongodb mongoose?如何解决 mongodb mongoose 中的“TypeError: db.find(...).toArray is not a function”?
【发布时间】:2018-12-01 16:22:50
【问题描述】:

我正在尝试查询我的 mongodb 集合以更新字段,但在尝试使用 .toArray 循环浏览所有文档时遇到错误。

每当我运行下面的代码时,它都会显示TypeError: meme.find(...).toArray is not a function

我做错了什么,我能做些什么来解决它?

var mongoose = require('mongoose');
const mongoolia = require('mongoolia').default;
var algoliasearch = require('algoliasearch');

//meme schema
var meme = require('../app/model/meme');

const TagSchema = new mongoose.Schema({
  tagarray: { type: String, required: true, algoliaIndex: true },
});

TagSchema.plugin(mongoolia, {
  appId: 'QQVL4GOPOL',
  apiKey: 'd93766f242500de7a13183eeeb6a7934',
  indexName: 'test1'
})

meme.find().toArray(function(memes){
   meme.update({_id: memes._id}, {$set: { objectID: memes._id.toString().slice(10).slice(0,24)}}); 
}); 

【问题讨论】:

标签: node.js mongodb mongoose


【解决方案1】:

find().toArray() 更改为find().fetch();

【讨论】:

【解决方案2】:

试了很多次才发现

 db.collection(collection).find(data, (err, res) => err ? reject(err) : res.toArray().then(x => resolve(x)))

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 2018-09-01
    • 2018-11-14
    • 2021-07-02
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    相关资源
    最近更新 更多