How to use the mongo function from mongoose

Find comprehensive JavaScript mongoose.mongo code examples handpicked from public code repositorys.

mongoose.mongo is a sub-package in the Mongoose library that provides access to the underlying MongoDB driver.

212
213
214
215
216
217
218
219
220
221
You may optionally assign the driver directly to the `gridfs-stream` module so you don't need to pass it along each time you construct a grid:

```js
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;

var conn = mongoose.createConnection(..);
conn.once('open', function () {
  var gfs = Grid(conn.db);
fork icon129
star icon610
watch icon32

+ 2 other calls in file

-2
fork icon31
star icon279
watch icon16

+ 3 other calls in file

How does mongoose.mongo work?

mongoose.mongo is a sub-package in the Mongoose library that provides a way to access the underlying MongoDB driver from within Mongoose.

When working with MongoDB, there are times when you need to access features that are not available through Mongoose's API. In these cases, you can use mongoose.mongo to access the MongoDB driver directly.

For example, you might use mongoose.mongo to perform a bulk write operation that is not supported by Mongoose's Model API. Or you might use it to directly access the Db object for a connection, so you can execute commands such as createIndex or stats.

To use mongoose.mongo, you first need to get a reference to the underlying Db object. You can do this by calling the connection.db method on a Mongoose Connection object. Once you have the Db object, you can use it to access the full range of functionality provided by the MongoDB driver.

Overall, mongoose.mongo provides a way to access the underlying MongoDB driver from within Mongoose, giving you more flexibility and control over your MongoDB interactions.

45
46
47
48
49
50
51
52
53
54
.then(x => {
    console.log(nameFile + ` | Connected to Mongo! Database name: "${x.connections[0].name}"`);
    logger.info(nameFile + ` | Connected to Mongo! Database name: "${x.connections[0].name}"`);
    db = x.connections[0].db;
    //console.log(x.connections[0].db);
    gridFSBucket = new mongoose.mongo.GridFSBucket(x.connections[0].db, {
        bucketName: "fs"
    });
    storage = new GridFsStorage({
        url: mongoURI,
fork icon1
star icon4
watch icon5

+ 4 other calls in file

3336
3337
3338
3339
3340
3341
3342
3343
3344
3345

updateCart: async function(req,res) {
    let cart = req.body.cart;
    let model = await this.createModel(`${req.params.brand}-cart`);
    cart =  cart.map( val => {
        val._id = val._id == '' ? new mongoose.mongo.ObjectID() : mongoose.mongo.ObjectID(val._id);
        return val;
    });
    let output = await Promise.all( cart.map( (val, index) => model.findOneAndUpdate( {_id: val._id}, val, { new: true, upsert: true } ) ) ); 
    return {success: 'done', output: output}
fork icon0
star icon2
watch icon1

+ 6 other calls in file

Ai Example

1
2
3
4
const mongoose = require("mongoose");

const objectId = new mongoose.mongo.ObjectID();
console.log(objectId);

This will log a newly created ObjectID to the console, which will look something like this: bash Copy code

18
19
20
21
22
23
24
25
26
27
28
29
let gfs, gridfsBucket;


const conn = mongoose.createConnection(process.env.MONGO_URL_FILE_UPLOADS)


conn.once('open', () => {
    gridfsBucket = new mongoose.mongo.GridFSBucket(conn.db, {
        bucketName: 'uploads'
    });
    gfs = Grid(conn.db, mongoose.mongo);
    gfs.collection('uploads');
fork icon0
star icon0
watch icon1

+ 4 other calls in file

16
17
18
19
20
21
22
23
24
25
26
  dbName: process.env.DB_NAME
});


let gfs;
conn.once('open', () => {
  gfs = new mongoose.mongo.GridFSBucket(conn.db, {
    bucketName: 'images',
  });
});

fork icon0
star icon0
watch icon1