Why are db collections created for Mongoose 6 subdocs? - mongodb

I'm looking at upgrading my projects mongoose version from 5.10.19 to latest (6.5.1). I'm noticing that I have a lot more collections in my database than I did before. I made a simple example to test this out and when I run it on mongoose 5, I only see the collection "mains" but mongoose 6 creates "mains" and "subs". I'd expect that the subdocument models would not have their own collection like mongoose 5 behaves.
import { connect, model, Schema } from 'mongoose';
const mongoUrl = 'mongodb://localhost:27017/test';
(async () => {
const subSchema: Schema = new Schema({ color: String, yes: Boolean });
const mainSchema: Schema = new Schema({ name: String, sub: subSchema });
const MainModel = model('Main', mainSchema);
model('Sub', subSchema);
await connect(mongoUrl, { ssl: true, sslValidate: false });
console.log(`Successfully connected to mongodb: "${mongoUrl}"`);
await MainModel.create({ name: 'One', sub: { color: 'Yellow', yes: true } });
})()
.then(() => {
console.log('\nSuccess');
process.exit();
})
.catch(() => {
console.log('\nFailure');
process.exit();
});
Is there a mongoose setting I'm missing that's causing this to happen?
Also, on Node 12.20.12.

It's probably because of this change:
autoCreate is true by default unless readPreference is secondary or secondaryPreferred, which means Mongoose will attempt to create every model's underlying collection before creating indexes.
So turning off autoCreate (and possibly autoIndex) on subSchema should fix it. Alternatively, just don't create a model of it (only the schema).

Related

Mongoose not creating document on mongodb

I am using Next.js and MongoDB (mongoose) to make an education portal website. The school admin can add important notices from their dashboard and the student can view them from theirs. Most mongoose operations have to be done in an API that next.js provides them self so my code to create the document on MongoDB is located in /API/ directory. Code:
/**
* #param {import("next").NextApiRequest} req
* #param {import("next").NextApiResponse} res
*/
import circulars from '../../../model/hw'
export default async function stuffss(req, res) {
try{
const {number, text} = req.body
console.log("Connecting to mongo")
mongoose.connect("mongodb+srv://usernameherer:passowordhere#cluster0.clusteridhere.mongodb.net/stuff?retryWrites=true&w=majority")
console.log("Connected to mongo")
console.log("Creating document")
const createdCircular = await circulars.create(req.body)
console.log("Created document")
res.json({ createdCircular })
}
catch(error) {
console.log(error)
res.json({ error })
}
}
The circulars schema:
const mongoose = require('mongoose')
const circularsSchema = new mongoose.Schema({
number: {
unique: true,
required: true,
type: Number
},
text: {
required: true,
type: String
}
})
const circulars = mongoose.models.circularss || mongoose.model("circularss", circularsSchema)
export default circulars
Sending a post request using insomnia gives the following result:
{
"createdCircular": {
"number": 15463,
"text": "loreum ipsum",
"_id": "63cb86421ca543a731cb39c3",
"__v": 0
}
}
but when I go and look for it on the MongoDB website it doesn't have any results.
I believe it may be because of how I am connecting or that I am not sending it properly to a collection.
This is the structure of my database:
Is there a way to connect to the 'stuff' database and 'circulars' collection?
Looks like that the database needs to be specified in connection string.
E.g.
...clusteridhere.mongodb.net/stuff?retryWrites=true&w=majority
Ref: https://mongoosejs.com/docs/connections.html
Here is how I fixed this issue. In my schema I had put the same circulars which created created a new collection for it, I also had to remove await from mongoose.connect. This combined fixed the issue for me. You should also make sure that mongoose.models.name is the same as the mongoose.model("name", schema)

Mongoose adding url slug to the returned JSON object

Sorry in advance if my formatting is off! I'm building a project using MongoDB, Mongoose, and Express. Right now I'm trying to use Mongoose to read a document from the MongoDB database. For some reason it's prepending the word "slug" to the document I'm fetching from the database.
The result I'm getting is this:
[{"slug":"","title":"test","id":"62002ba44b05edb74c1a9cd8"}]
When the result I should be getting is this: [{"title":"test","id":"62002ba44b05edb74c1a9cd8"}]
I'm thinking there's an unexpected side effect from one of the libraries I'm using but I can't figure out what's causing it. It's like this before I call res.render("Test", testRes) so it might even be coming from the database like this somehow? I've been stumped on this for hours now.
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Test = mongoose.model('Test');
const sanitize = require('mongo-sanitize');
router.get('/test/:testSlug', async function(req, res) {
const testSlug = req.params.testSlug;
const testSearchParam = {
slug: sanitize(testSlug)
};
console.log("search param", testSearchParam.slug);
const testRes = await Test.find(
{
title: testSearchParam.slug
}
);
res.render("Test", testRes);
});
module.exports = router;
and here is my schema for the Test data format:
const mongoose = require('mongoose');
const URLSlugs = require('mongoose-url-slugs');
const { toJSON } = require('../plugins');
const TestSchema = new mongoose.Schema({
title: {
type: String,
required: true
}},
{
collection: 'test'
}
);
TestSchema.plugin(URLSlugs('title'));
TestSchema.plugin(toJSON);
/**
* #typedef Test
*/
const Test = mongoose.model('Test', TestSchema);
module.exports = Test;
Since you are using mongoose-url-slugs, the package has a default option to create a slug field in mongoose schema.
addField (Default: True) - Add slug field to mongoose schema.
See here: https://www.npmjs.com/package/mongoose-url-slugs#options-and-defaults

How to Connect/Query Mongoose to an existing Database

I am working on trying to connect a Node/Express server to an existing MongoDB Database/Collection. I have already successfully connected to the database. However, I am having a tremendously difficult time setting up my models/schema to query.
The MongoDB is MongoDB Atlas and has one collection with over 800,000 documents. The name of the single collection is "delitosCollection".
I have tried the following to with no success:
var CrimeData = mongoose.model('DelitosCollection', new Schema({}),'delitosCollection');
mongoose.connection.on('open', function(ref){
console.log("connected to the mongo server");
CrimeData.find({}, (err,results) => {
if(err){
console.log("ERROR")
throw err
}
console.log("results: ", results.length)
} )
});
I know the connection is working as I am receiving the console.log with no errors. However, results.length is returning 0 when it should be over 800,000. Have spent way too many hours on this.
Create an empty schema for each collection you want to use
and then create a model to be used in your project
the model take 3 parameter
1)name of the model
2)schema name
3)collection name ( from mongodb atlas)
like that
const mongoose = require('mongoose');
mongoose.connect('mongodb uri')
const userSchema = new mongoose.Schema({});
const User = mongoose.model('User', userSchema, 'user');
then you can use the model normally
User.find({})
connection to mongo db
// Connect to mongoDB
mongoose.connect('mongodb://localhost/[yourDbName]',{useNewUrlParser:true})
.then(function(){
console.log('mongoDB connected');
})
.catch(function(){
console.log('Error :');
})
after that you will have to create your schema and then only you can query the database
create your schema like this
// Crimes Schema
const CrimeDetailsSchema= new Schema({
first_name: {
type: String,
required: true
},
last_name: {
type: String,
required: true
},
email: {
type: String,
required: true
}
});
const Profile = module.exports = mongoose.model('delitosCollection', CrimeDetailsSchema, 'delitosCollection');
after that create your queries
you can get an idea about that in mongoose documentation here
You can refer to the answer given below, just pass an empty object in schema
like db.model('users', new Schema({}))

Mongoose - save after lean

I'm fetching an object from mongo using mongoose but i want to use lean as there are attributes that aren't in mongoose model (strict:false)
After modifying that object i can't save it back
let order = await Order.findOne({_id: req.body.id}).populate({path: 'o_s', match: {removed: false}}).lean()
order.save() //not working as it's not mongoose model any more
Order.update({_id:order._id},{order}).exec(function(){
return res.json({status: true, message: 'Order Updated', order: order});
}) //not giving error but not updating too
any suggestions ?
In order to update the doc you retrieved from the db, you have to remove the _id property.
Consider this contrived example:
#!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const schema = new Schema({
name: String
});
schema.set('strict', false);
const Test = mongoose.model('test', schema);
const test = new Test({
name: 'Henry',
nick: 'Billy'
});
async function run() {
await conn.dropDatabase();
await test.save();
let doc = await Test.findOne({}).lean();
const id = doc._id;
delete doc._id;
doc.name = 'Billy';
doc.nick = 'The Kid';
await Test.update({ _id: id }, doc);
let updated = await Test.findOne({});
console.log(updated);
return conn.close();
}
run();
output:
{ _id: 5ad34679bc95f172c26f3382,
name: 'Billy',
nick: 'The Kid',
__v: 0 }
note that you lose auto versioning in this scenario, if this is important to you, you'll need to manually increment the version property.
i suggest to doesn't use lean() because it has some performance isues that i realized.
otherwise you can use JSON.parse(JSON.stringify(order)) and for some reasean it's a better approach. especially wen you want to iterate over the result ( in my case it was two times faster for iteration).

How to access a preexisting collection with Mongoose?

I have a large collection of 300 question objects in a database test. I can interact with this collection easily through MongoDB's interactive shell; however, when I try to get the collection through Mongoose in an express.js application I get an empty array.
My question is, how can I access this already existing dataset instead of recreating it in express? Here's some code:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
mongoose.model('question', new Schema({ url: String, text: String, id: Number }));
var questions = mongoose.model('question');
questions.find({}, function(err, data) { console.log(err, data, data.length); });
This outputs:
null [] 0
Mongoose added the ability to specify the collection name under the schema, or as the third argument when declaring the model. Otherwise it will use the pluralized version given by the name you map to the model.
Try something like the following, either schema-mapped:
new Schema({ url: String, text: String, id: Number},
{ collection : 'question' }); // collection name
or model mapped:
mongoose.model('Question',
new Schema({ url: String, text: String, id: Number}),
'question'); // collection name
Here's an abstraction of Will Nathan's answer if anyone just wants an easy copy-paste add-in function:
function find (name, query, cb) {
mongoose.connection.db.collection(name, function (err, collection) {
collection.find(query).toArray(cb);
});
}
simply do find(collection_name, query, callback); to be given the result.
for example, if I have a document { a : 1 } in a collection 'foo' and I want to list its properties, I do this:
find('foo', {a : 1}, function (err, docs) {
console.dir(docs);
});
//output: [ { _id: 4e22118fb83406f66a159da5, a: 1 } ]
You can do something like this, than you you'll access the native mongodb functions inside mongoose:
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/local');
var connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', function () {
connection.db.collection("YourCollectionName", function(err, collection){
collection.find({}).toArray(function(err, data){
console.log(data); // it will print your collection data
})
});
});
Update 2022
If you get an MongoInvalidArgumentError: The callback form of this helper has been removed. error message, here's the new syntax using async/await:
const mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/productsDB');
const connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', async function () {
const collection = connection.db.collection("Products");
collection.find({}).toArray(function(err, data){
console.log(data); // it will print your collection data
});
});
I had the same problem and was able to run a schema-less query using an existing Mongoose connection with the code below. I've added a simple constraint 'a=b' to show where you would add such a constraint:
var action = function (err, collection) {
// Locate all the entries using find
collection.find({'a':'b'}).toArray(function(err, results) {
/* whatever you want to do with the results in node such as the following
res.render('home', {
'title': 'MyTitle',
'data': results
});
*/
});
};
mongoose.connection.db.collection('question', action);
Are you sure you've connected to the db? (I ask because I don't see a port specified)
try:
mongoose.connection.on("open", function(){
console.log("mongodb is connected!!");
});
Also, you can do a "show collections" in mongo shell to see the collections within your db - maybe try adding a record via mongoose and see where it ends up?
From the look of your connection string, you should see the record in the "test" db.
Hope it helps!
Something else that was not obvious, to me at least, was that the when using Mongoose's third parameter to avoid replacing the actual collection with a new one with the same name, the new Schema(...) is actually only a placeholder, and doesn't interfere with the exisitng schema so
var User = mongoose.model('User', new Schema({ url: String, text: String, id: Number}, { collection : 'users' })); // collection name;
User.find({}, function(err, data) { console.log(err, data, data.length);});
works fine and returns all fields - even if the actual (remote) Schema contains none of these fields. Mongoose will still want it as new Schema(...), and a variable almost certainly won't hack it.
Go to MongoDB website, Login > Connect > Connect Application > Copy > Paste in 'database_url' > Collections > Copy/Paste in 'collection' .
var mongoose = require("mongoose");
mongoose.connect(' database_url ');
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function () {
conn.db.collection(" collection ", function(err, collection){
collection.find({}).toArray(function(err, data){
console.log(data); // data printed in console
})
});
});
I tried all the answers but nothing worked out, finally got the answer hoe to do it.
var mongoose = require('mongoose');
mongoose.connect('mongodb://0.0.0.0:27017/local');
// let model = require('./test1');
setTimeout(async () => {
let coll = mongoose.connection.db.collection(<Your collection name in plural form>);
// let data = await coll.find({}, {limit:2}).toArray();
// let data = await coll.find({name:"Vishal"}, {limit:2}).toArray();
// let data = await coll.find({name:"Vishal"}, {projection:{player:1, _id:0}}).toArray();
let data = await coll.find({}, {limit:3, sort:{name:-1}}).toArray();
console.log(data);
}, 2000);
I have also mentioned some of the criteria to filter out. Delete and update can also be done by this.
Thanks.
Make sure you're connecting to the right database as well as the right collection within the database.
You can include the name of the database in the connection string.
notice databasename in the following connection string:
var mongoose = require('mongoose');
const connectionString = 'mongodb+srv://username:password#hosturl.net/databasename';
mongoose.connect(connectionString);