0

I created one schema in mongodb its very nested schema. It looks like this

{
    "_id": ObjectID("559690ec34c506cea4be1775"),
    "cities": [
        {
            "val": "something",
            "pincode": [
                {
                    "val": "something",
                    "people": [
                        {
                            "val": "something",
                            "frnds": [
                                {
                                    "val": "something1",
                                    "frndaddress": [
                                        {
                                            "val": "something2"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

This document is inserted properly in mongodb but I don't have any idea about how I can convert this is on mongoose I try this one with mongoose but its seems its not working

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

// create a all city list, without id
var allCitySchema = new Schema({
    cities: [{
        val:{ type: String },
        pincode:[{
                val:{ type: String },
                people:[{
                      val:{ type: String },  
                      frnds:[{
                              val:{ type: String },
                              frndaddress:[{
                                 val:{ type: String },     
                              }]
                      }]  
                }]
        }]
    }]
}, {collection: 'allcities'});

var allcities = mongoose.model('allcities', allCitySchema);
module.exports = allcities;

I am new on node and mongodb I created above schema I even don't know its correct or not.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dexter
  • 1,804
  • 4
  • 24
  • 53

2 Answers2

3

I would suggest that you should try flattening out your data structure. It really depends on how all of these fields are connected, however, a good rule of thumb from my experience is that linking objects in your mongoDB via reference array is a great way to hold a relation but still allow a high degree of decoupled information. Here is one example of how you could set your Schemas for the above but still retain the relations you want. First a schema for each city:

// A Schema for city information
var mongoose = require('mongoose');
var People = require('./people.js');

var City = new mongoose.Schema({
//Pincode for this city object
pincode: { type: String },
// An array of references to the people objects for those who live in this city
people: [{ type: mongoose.Schema.Types.ObjectId, ref: 'People' }]
});

module.exports = mongoose.model('City', City);

Then in a separate file, a Schema for people:

// A Schema for person information
var mongoose = require('mongoose');
var Friends = require('./people.js');

var Person = new mongoose.Schema({
    //The address of this Person object
    address: { type: String },
    // An array of references to the other people objects who are friends of this person
    friends: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Friends' }]
    });

    module.exports = mongoose.model('People', People);

Notice that friends are also People objects. This kind of structuring of data has several advantages. 1) All fields only have one place in the database and therefore use the least amount of disk space since there is very little (only reference IDs) redundancy. 2) Retrieving information from several Schemas via a DB query is fairly easy with mongoose through mongoose population, and deep population.

Community
  • 1
  • 1
ZeroSum
  • 165
  • 2
  • 4
0

usually, there is two way whether you make a relationship or you can create sub docs

MD SHAYON
  • 7,001
  • 45
  • 38