0

Firstly, I wrote the code to save fruit and person documents in my database. And then I add the last few lines to read all my fruit documents. This is my JavaScript code:

// getting-started.js
const mongoose = require('mongoose');

main().catch(err => console.log(err));

async function main() {
  await mongoose.connect('mongodb://localhost:27017/test');

  // use `await mongoose.connect('mongodb://user:password@localhost:27017/test');` if your database has auth enabled
}

//create a SCHEMA that sets out the fields each document will have and their datatypes
const fruitSchema = new mongoose.Schema({
  name: String
});

//create a MODEL
const Fruit = mongoose.model('Fruit', fruitSchema);

//create a DOCUMENT
const fruit = new Fruit({
  name: 'Apple',
  rating: 7,
  review: "Great!"
});

//save the document
fruit.save();
//(we comment it in case you do not want to save the same thing every time)

const personSchema = new mongoose.Schema(
  {
    name:String,
    age:Number,
  }
);

const Person = mongoose.model('Person', personSchema);

const person = new Person({
  name:"Alice",
  age:45
})

//person.save();

const kiwi = new Fruit({
   name: 'Kiwi',
   score: 10,
   review: 'The best fruit!'
 });

 const orange = new Fruit({
   name: 'Orange',
   score: 4,
   review: 'Too sour for me'
 });

 const banana = new Fruit({
   name: 'Banana',
   score: 3,
   review: 'Weird texture'
 });

 /*Fruit.insertMany([kiwi, orange, banana], function(err){
    if (err) {
      console.log(err);
    } else {
      console.log('Successfully saved all the fruits to FruitsDB');
    }
  });*/

  Fruit.find(function(err,fruits){
    if(err){
      console.log(err);
    }
    else{
      console.log(fruits);
    }
  })

And this my running result:

$ node app.js
[
  {
    _id: new ObjectId("6349856a38dadec09142ade8"),
    name: 'Kiwi',
    __v: 0
  },
  {
    _id: new ObjectId("6349856a38dadec09142ade9"),
    name: 'Orange',
    __v: 0
  },
  {
    _id: new ObjectId("6349856a38dadec09142adea"),
    name: 'Banana',
    __v: 0
  },
  {
    _id: new ObjectId("63498906b285b3fc1887f99f"),
    name: 'Apple',
    __v: 0
  },
  {
    _id: new ObjectId("63498906b285b3fc1887f9a1"),
    name: 'Kiwi',
    __v: 0
  },
  {
    _id: new ObjectId("63498906b285b3fc1887f9a2"),
    name: 'Orange',
    __v: 0
  },
  {
    _id: new ObjectId("63498906b285b3fc1887f9a3"),
    name: 'Banana',
    __v: 0
  },
  {
    _id: new ObjectId("634a2e34d99e79b07647f88f"),
    name: 'Apple',
    __v: 0
  }
]

You can see there is no review and rate in the result. Just the names appear. I wonder why it would be like this.

1 Answer 1

1

You did not include does properties in the Schema definition. Try changing this part:

const fruitSchema = new mongoose.Schema({
  name: String
});

To this:

const fruitSchema = new mongoose.Schema({
  name: String,
  rating: Number,
  review: String
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.