Set unique key in mongoose - node.js

I'm currently using the following code. I'm basically using the example from the mongoose documentation but I'm trying to get the name field to be unique
here I am intentionally creating two unique "kittens" and trying to save both of them
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/test", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", function () {
console.log("Connected to database");
});
const kittySchema = new mongoose.Schema({
name: { type: String, unique: true },
});
kittySchema.methods.speak = function () {
const greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
};
const Kitten = mongoose.model("Kitten", kittySchema);
const fluffy = new Kitten({ name: "fluffy" });
fluffy.save();
const fluffy2 = new Kitten({ name: "fluffy" });
fluffy2.save();
Using the unique property doesn't seem to work because I keep getting duplicate entries in the database. I've also tried using the removedups key in the schema which I believe has been deprecated.

This property unique is not for data to be saved uniquely. It's for unique indexing that will be applied on field.
You can chech documentation here.
https://joshtronic.com/2018/06/07/unique-indexes-with-mongodb-and-mongoose/

Related

Nodejs: not getting any data while fetching data from MongoDB?

I am trying to fetch data from MongoDB by a field name - pubdate, but data is not showing neither I am getting any error!
I have field in the collection - _id, article, headline, pubdate all are String type except _id which is Objectid.
When I tried this query in Mongo query browser like - compass and studio 3t I got data -
{ pubdate: { '$gte': '2022-12-01', '$lte': '2022-12-31' } }
I am using postman to fetch data, in raw option sending POST request in JSON form.
{"fdate":"2022-12-31","tdate":"2022-12-31"}
const express = require("express");
const app = express();
const port = 3005;
const mongoose = require("mongoose");
// Connect to MongoDB using Mongoose
const url =
"mongodb://localhost:2701/db";
mongoose.connect(url, { useUnifiedTopology: true, useNewUrlParser: true });
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
// Define the Article schema
const articleSchema = new mongoose.Schema({
headline: String,
fulltext: String,
pubdate: String,
article_type: String,
});
const Article = mongoose.model("collectioname", articleSchema);
// Route for retrieving articles based on fromdate and todate
app.post("/articles2", (req, res) => {
let _fDate = req.body.fdate;
let _tDate = req.body.tdate;
Article.find({
pubdate: { $gte: _fDate, $lte: _tDate },
}).exec((err, articles) => {
if (err) {
console.error(err);
res.status(500).send("Error retrieving articles");
return;
}
res.send(articles);
});
});
// Start the server
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
How Will I pass the value in Postman to get the record, I am clueless as of now?
I tried map function too but no output-
let _articles = articles.map((x) => x.articleid); res.send(_articles);
Your issue is caused by Mongoose, by default, lowercasing and pluralizing the name that you pass when creating the model to determine the name of the collection it will use for the model.
In other words, when you use mongoose.model('User', UserSchema), Mongoose will take the name (User) and lowercase and pluralize it, so the collection becomes users.
If you want Mongoose to use an existing collection, or if you want to have full control of the collection name, you need to use the collection option in your schema:
const articleSchema = new mongoose.Schema({
headline: String,
fulltext: String,
pubdate: String,
article_type: String,
}, {
collection : 'article_fulltext' // the collection to use for this schema
});
const Article = mongoose.model("Article", articleSchema);
Without this option, and assume that you used article_fulltext as the model name, Mongoose would have used a collection named article_fulltexts.
Can you try logging what articles return in exec callback?
One possible issue could be that variables _fDate and _tDate are not seen as Dates by mongoose.So try wrapping them with new Date?
Article.find({
pubdate: { $gte: new Date(_fDate), $lte: new Date(_tDate) },
})

How does mongoose know what collection I am accessing?

I'm having a trouble grasping a concept in Mongoose.
I'm using MongoDB atlas, got a cluster , a database and 2 collections.
users, characters.
Through a guide I've learned that a good way to write your stuff is to have a model (I use the naming schema) as a file, importing it into your Database module/class
and using it there to perform a query...
const mongoose = require("mongoose");
const process = require("./config.env");
db = () => {
return mongoose
.connect(process.env.URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: true,
})
.then((response) => {
console.log(`Connected to Databse : ${response.connection.host}`);
})
.catch((err) => {
console.log("DB_ERROR:", err);
process.exit(1);
});
};
module.exports = db;
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
});
const User = mongoose.model("User", UserSchema);
module.exports = User;
const User = require("../schemas/User");
const db = require("../config/db");
class Database {
constructor(db, collection) {
this.db = db;
this.collection = collection;
this.User = User;
}
connect() {
return db();
}
}
module.exports = Database;
one file to handle the db connection..another file as the User schema and a third file to handle every function i might use globally...
One thing I cannot wrap my mind around is
how is the findOne() function able to locate the collection I am using without me telling it what collection i want it to search in?
is it somehow translating the
const User = mongoose.model("User", UserSchema);
line and searching for "users" as well? I just can't understand the magic behind this...
what if I want to search specifically in the characters collection...?
Mongoose uses the model name, as passed when it was created: mongoose.model("User", UserSchema), converted to lower case and with an 's' appended.
For the model User it uses the collection users by default. You can change this by explicitly specifying the collection name in the schema.

Beginner Issue with Mongoose and MongoDB

I'm new to Node, along with Mongoose and MongoDB. I'm trying to test inserting data into one of the collections in a database on MongoDB Atlas. However, the code somehow inserts the data into the wrong database. I intend to insert data into the 'test' collection in the 'quizzard' database. However, a new collection called 'tests' was created within quizzard where the data was placed. When I tried it again, it started inserting data into another database called 'test' and created a collection called 'tests', where the data is still being placed.
var link = "mongodb+srv://<user>:<password>#quizzard-dp0b2.mongodb.net/test?retryWrites=true&w=majority";
// changed to <user> and <password> for privacy reasons
mongoose.connect(link, {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.on('connected', () => {
console.log('Connected');
});
const Schema = mongoose.Schema;
const TestSchema = new Schema({
_id: Number,
data: String
});
//TestSchema.set('database', 'test');
//TestSchema.set('collection', 'test');
const Test = mongoose.model('Test', TestSchema);
const data = {
_id: 11,
data: "why???"
};
const newTest = new Test(data);
newTest.save((error) => {
if(error){
console.log("An error has occured");
} else {
console.log("Action performed");
}
});
You need to change the link; after the first slash, you choose which DB you want to use.
Examples
// DB NAME youinsertinheredbname
var link = "mongodb+srv://<user>:<password>#quizzard-dp0b2.mongodb.net/youinsertinheredbname?retryWrites=true&w=majority";
// DB NAME stackoverflow
var link = "mongodb+srv://<user>:<password>#quizzard-dp0b2.mongodb.net/stackoverflow?retryWrites=true&w=majority";

How to modulize my mongoose setup

This use of db.model('Model_Name').find(...) worked before, but I've since split my database, model and controllers up, so now it seems I need to change things around Please help show where I'm going wrong:
schema.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var UsersSchema = new Schema({
display_name : String,
email : String
});
var ClientErrorsSchema = new Schema({
datetime : Number,
msg : String,
url : String,
line : Number
});
var users = mongoose.model('Users',UsersSchema);
var client_errors = mongoose.model('Client_Errors',ClientErrorsSchema);
module.exports = mongoose.model;
db.js
var mongoose = require('mongoose');
var model = require("./schema.js");
var MONGO = {
username: "admin",
password: "***",
server: 'localhost',
port: '*****',
db: 'db_name',
connectionString: function(){
return 'mongodb://'+this.username+':'+this.password+'#'+this.server+'/'+this.db;
},
options: {
server:{
auto_reconnect: true,
poolSize: 20,
socketOptions:{
keepAlive: 1
}
},
db: {
numberOfRetries: 10,
retryMiliSeconds: 1000
}
}
};
var db = mongoose.createConnection(MONGO.connectionString(), MONGO.options);
db.model = model;
db.on('error', function(err) {
f.consoleLogger("DB connection Error: "+err);
});
db.on('open', function() {
f.consoleLogger("DB connected");
});
db.on('close', function(str) {
f.consoleLogger("DB disconnected: "+str);
});
module.exports = db;
user.js
var db = require("./db.js");
...
db.model('Users').find(...)
So using the util.inspect, I can see the db has all of the models, but the error I'm getting on the find is TypeError: Cannot read property 'User' of undefined'.
UPDATE
Toad22222 provided great advice, and the schema appears to be valid - but the queries are not working - neither db.model.Users.find({...},function(err,data){...}); or db.model.Users.find({...}).exec(function(err,data){...}); fires the callback. I have put the same code in the on('open') event as well, nothing happens. Hopefully someone can explain why.
All advice appreciated!
Try out
module.exports = {
User: users,
ClientErrors: client_errors
}
instead of
module.exports = mongoose.model;
You want to be exporting the schemas you are creating, not the mongoose.model datatype.

I am getting an error of mongoose is not defined

I am creating an api using MongoDB, I am using Mongoose to create Data Persistence. However I am getting an error that Mongoose is not defined, I have used require function to call the node module but it is still giving me the same error.
Below is the connection file
var mongoose = require('mongoose')
var database = 'api'
const server = 'mongodb://localhost:27017/'+database
console.log(server)
mongoose.connect(server)
const db = mongoose.connection
console.log(db)
var Schema = mongoose.Schema
var ObjectId = Schema.ObjectId
const WeatherSchema = new Schema({
id: {type: String, required: true},
name: { type: String, required: true },
items: {type: String, required: true}
})
var WeatherDB = mongoose.model('DBlist', WeatherSchema)
You should wait for the database to connect, as it doesn't happen immediately. Something like this:
var mongoose = require('mongoose');
mongoose.connect(sever);
var db = mongoose.connection;
db.on('disconnect', connect); // auto reconnecting
db.on('error', function(err) {
debug('connection error:', err);
});
db.once('open', function (callback) {
// we're in the game, start using your Schema
const WeatherSchema = new Schema({...
});
p.s.
I've added little extra sugar just to let you know these events exist and are quite helpful to understand what's going on.

Resources