Search code examples
node.jsmongodbmongoose

Mongoose find method don't work (no errors)


So I tried everything even tried to change to mongodb but it still never resolves

I'm using a simple code like this for connection and searching

Notes: all the methods are working and this code just stopped working randomly (i'm not joking i sleept and it was normal when I woke up it died), the find method doesn't work when I do give a query to take all the documents, but it works when I do not get everything, when I do not give a param it doesn't work as well {} doesn't work, the date param is a empty string on all docs '' and if I do pass this a param in order to get all docs, it never resolves too, also tried using callbacks on find, no errors, nothing, updated packages and nothing too.

import { connect, set } from 'mongoose'
import config from './config.json' assert {type: 'json'}
import { TwitterUserInfo } from './src/models/twitter_user_info.js'

set("strictQuery", true)
await connect(config.MONGO_URI || "", { keepAlive: true, autoIndex: false, writeConcern: { w: "majority" } })
    .then(res => console.log("[!] DataBase status: ONLINE"))
    .catch(err => console.log("DataBase login err: " + err))

const basicTest1 = await TwitterUserInfo.find({ influencers: [] }) // works!
console.log(basicTest1) // and logs the docs matching

const dataSet = await TwitterUserInfo.find() // never resolves
console.log(dataSet) // never logged bcs the find never ends

And this is the schema

import { Schema, model } from 'mongoose'

const twitterUser = new Schema({

    id: { type: String, unique: true, required: true },
    username: { type: String, unique: true, required: true },

    followers: { type: Array, default: [] },
    followersCount: { type: Number, default: 0 },
    influencers: { type: Array, default: [] },
    date: { type: String, default: "" }

});
export const TwitterUserInfo = model("TwitterUserInfo", twitterUser)

npm versions:

{                                 
  'mybots-app-source-v3': '1.3.0',
  npm: '8.11.0',                  
  node: '16.15.1',                
  v8: '9.4.146.24-node.21',       
  uv: '1.43.0',                   
  zlib: '1.2.11',                 
  brotli: '1.0.9',                
  ares: '1.18.1',                 
  modules: '93',                  
  nghttp2: '1.47.0',              
  napi: '8',                      
  llhttp: '6.0.4',                
  openssl: '1.1.1o+quic',         
  cldr: '40.0',                   
  icu: '70.1',                    
  tz: '2021a3',                   
  unicode: '14.0',                
  ngtcp2: '0.1.0-DEV',            
  nghttp3: '0.1.0-DEV'            
}                                 

Solution

  • Just to save there, the problem was that my mongo had a 50gb+ JSON archive and I was using the .find() method witch retrives everything and was taking at least 1hour up to 2h, the solution was using keys and query optmization that saved the project.