arashsheyda / nuxt-mongoose

A Nuxt module for simplifying the use of Mongoose in your project.
https://docs.arashsheyda.me/nuxt-mongoose
76 stars 14 forks source link

How to get the connection sample #4

Closed Ena-Heleneto closed 1 year ago

Ena-Heleneto commented 1 year ago

I need to use mogoose for query all Collection

Based on the available intelligence, I can do this

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true });

const db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function() {
  // we're connected!
  console.log("Connected to database");

  // get a list of all collections
  db.db.listCollections().toArray(function(err, collections) {
    console.log(collections);
  });
});

but in nuxt-mogoose , I can`t get the connection sample to call the db.listCollections function

arashsheyda commented 1 year ago

@Abernethy-BY since nuxt-mongoose is mongoose, you can use mongoose directly. an example would be server/api/users.get.ts:

import mongoose from 'mongoose'

export default defineEventHandler(async (event) => {
  try {
    return mongoose.connection.db.listCollections().toArray()
  }
  catch (error) {
    return error
  }
})
Ena-Heleneto commented 1 year ago

thank you,I understood