Questions tagged [mongoose-schema]

Everything in the Mongoose ODM starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

Everything in the Mongoose ODM starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var blogSchema = new Schema({
  title:  String,
  author: String,
  body:   String,
  comments: [{ body: String, date: Date }],
  date: { type: Date, default: Date.now },
  hidden: Boolean,
 meta: {
  votes: Number,
  favs:  Number
 }
});

Related Links

3348 questions
0
votes
1 answer

Cannot assign type 'number[]' to type '[[number]]'

i have 2 models in mongoose , i want to assign values in the second model to the first one . the geometry representation in first model 1st ` geometry: { type: string; coordinates?: [[number]]; }; ` the geometry representation…
0
votes
1 answer

How to create mongo strict schema - nested doc with any string key and only values of array of ObjectID, as the typescript interface(SchemaForMongo):

I'm using Nest.js and trying to create a strict schema for the ts below: interface SchemaForMongo { [key: string]: ObjectID[] } const invalidDocumentProperty_1: SchemaForMongo = {validKey :…
ENcy
  • 324
  • 4
  • 7
0
votes
0 answers

Mongoose:Validation error path is required

**I'm trying to save a new document in mongodb with mongoose, but I am getting ValidationError: Path 'email' is required., Path 'passwordHash' is required., Path 'username' is required. even though I am supplying email, passwordHash and…
0
votes
0 answers

What is the best mongoose schema to store all the mails collection?

I'm developing a website application. In that I am storing user data and mails data. I am saving the first 15 emails in the "users" collection as a sub-document, and then storing the other mails in the "mails" collection. I am storing first 15 mails…
0
votes
1 answer

Need help figuring out why calling methods from a mongoose schema works fine in one part of my node.js app, but fails elsewhere

I have created the following user schema, including two methods: getSnapshot() getLastTweetId() user.js const mongoose = require('mongoose') const getLastTweetId = require('../utilities/getLastTweetId') const getFollowers =…
0
votes
0 answers

Perform a complex query in mongoose and return data in a specific format from the same collection

This is how the Order model is structured new Schema( { ... products: [ { _id: String, title: String, image: [String], category: String, purity: String, metal: String, weight:…
0
votes
1 answer

.save is not a function in Mongoose

What is wrong with the way I am using Mongo? I am simply trying to update a record. Here is my code: async function updateReview (req, res) { const review = { ...req.body } const existingReview = await Review.findOne({ userId:…
mcool
  • 457
  • 4
  • 29
0
votes
1 answer

How to load referenced document in Mongoose?

I have a MongoDB collection called categories which has a self-reference(parent) like follows. const schema = mongoose.Schema({ name: String, parent: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', } }); export…
Pol Santha
  • 17
  • 4
0
votes
0 answers

How to handle POSTing a multiple select with nothing selected and clearing the field in the mongodb document?

Using Node + Mongoose, I've got something that works, but I wonder if I'm missing an easier way to do this. I have a mongoose schema called ClipsConfiguration with the following: exclude_words: { type: [String], default: undefined, …
Andrew Johns
  • 705
  • 1
  • 6
  • 26
0
votes
0 answers

Cast to ObjectId failed for value "6370d877cec3e8b947ab3dc3 " (type string) at path "_id" for model "Item"

I am trying to delete a document by clicking on a checkbox, i am able to get its id but i am not able to delete it by use of function FindByIdAndRemove() app.post("/delete", function(req, res){ const checkedItemId = req.body.checkbox …
0
votes
1 answer

I'm trying to update the year in my MongDB

So this is my code router.put('/update/:year', async (req, res) => { const year = req.params.year; World.findByIdAndUpdate(year, req.body, {new:true}) .then(data => { if (!data) { res.status(404).send({ message:…
0
votes
1 answer

Problem with mongoose for findOne JSON object in JSON object

I have an application with data. They are stored in a mongo model. But when I try to find by username like this: ` app. get('/profile/:name', function(req, res) { var name = req.params.name ModelAccount.findOne({ user…
Laaw
  • 1
0
votes
1 answer

How can I pick values from one model and use them in another in nodejs using mongoose?

Here, I have a model named User ` import mongoose from "mongoose"; const Schema = mongoose.Schema; const userSchema = new Schema({ name: { type: String, required: true }, email: { type: String, required:…
Darsh. S.
  • 13
  • 3
0
votes
1 answer

How can I enforce uniqueness for a `set` of property values in a Schema?

I am messing around with MongooseJS and trying to create a friend request where I store a to and from key in MongoDB which are the userId's of the sender and receiver. I want to prevent duplicate requests from the same sender to the same…
Daly
  • 134
  • 5
0
votes
0 answers

parsem array of string data into json

I have a folder of .TXT extensions file with plain text in it how can I insert that plain text into the MongoDB database using the mongoose and fs module in node js const fs = require('fs'); var parsedata; var parsedTwice = []; let…
1 2 3
99
100