Home > database >  Can't add data to existing collection in MongoDB Atlas with mongoose
Can't add data to existing collection in MongoDB Atlas with mongoose

Time:08-08

I made a MongoDB Atlas account and then I made a cluster with a database called "Cubicle" and a collection in it called "Cubes" and when I connect to my MongoDB database and publish some data, MongoDB Atlas creates a new database for me named "Test" and collection "Cubes", but I want to add data to the already existing database and collection.

This is my code:

const mongoose = require('mongoose');

const cubeSchema = new mongoose.Schema({ name: { type: String, required: true }, description: { type: String, required: true, maxLength: 120 }, imageUrl: { type: String, required: true, validate: function(){ return this.imageUrl.startsWith('http'); } }, difficultyLevel: { type: Number, required: true, min: 1, max: 6 }, accessories: [ { type: mongoose.Types.ObjectId, ref: 'Accessory' } ] });

const Cube = mongoose.model('Cube', cubeSchema);

module.exports = Cube;

And here is my code in cubeService:

exports.create = (cube) => Cube.create(cube);

CodePudding user response:

First I would check your connection URI. Are you connecting to the proper DB in your cluster? I assume /test is a default database if you're not providing the connection URI explicitly.

  • Related