How to solve 'TypeError: db.find(…).toArray is not a function' in mongodb mongoose?

I’m trying to query my mongodb collection so as to update a field but am facing an error when trying to use .toArray to loop through all the documents.

Whenever I run the code below, it shows TypeError: meme.find(...).toArray is not a function.

What am I doing wrong and what can I do to solve it?

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)}}); 
});

Hi!

This question is not directly related to Algolia. But, in order to assist you, let me quote this answer from Stackoverflow:

The toArray function exists on the Cursor class from the Native MongoDB NodeJS driver. The find method in MongooseJS returns a Query object.

Feel free to click on the hyperlink provided above to find some code samples.

1 Like

The issue has been solved now. I appreciate your help, Joly.

1 Like