Mongoose Implement Dbref Find all sub-class information

Source: Internet
Author: User

Product table

var Mongoose = require(‘mongoose‘);var Schema = Mongoose.Schema;var Product = new Schema({    image : {              type : String          },    description : {                      type : String                  },    price : {                type : Number,                require : true            },    probability : {                      type : Number,                      require : true                  },    status :{                type : Number,                require : true,                1            },    categoryId:{        type:String,        require:true    },    name : {        type :String,        require : true    }},{    _id : true,    autoIndex : true});module.exports = Mongoose.model(‘Product‘,Product);

Product Classification Table

    var Mongoose = require(‘mongoose‘);var Schema = Mongoose.Schema;var Category = new Schema({    child : [{        type : Schema.Types.ObjectId,        ‘Product‘    }],    name : {                type : String,                require : true           },    description : {                type : String,                require : true            },    image : {                type : String,                require : true            }},{    _id : true,    autoIndex : true});module.exports = Mongoose.model(‘Category‘,Category);

We insert the ID of the item into the category each time we add a product.

var product = new Product ({name:' Jurassic Coffee ', Image:'/3.jpg ', Descript:"It's still possible.", Price:50.00, Probability: -, Status:1, CategoryId:"552f76bd3e0b2dfca7989da3"}) Product.save (function (err) {if(ERR)        {Console.log (ERR); }Else{Category.find ({_id:"552f76bd3e0b2dfca7989da3"},function (err,result) {result[0].child.push (product._id); result[0].save (function (err) {Console.log (' ok! ')                })            })        }    })

Find information about multiple items at the same time when looking for a categorical information

Category.find().populate(‘child‘).exec().then(function(result){        console.log(result);            })

Let's look at the output:

[{_id:552F76bd3e0b2dfca7989da3, Image:'/1.jpg ', Description:' Our coffee is from Brazil, safe and pollution-free, dry, eat, eat. ', Name:' Coffee ', __v:7, child: [{_id:552f76a9b396a1e8651bba8d, Status:1}, {_id:552F76FF61D8370BA8490CEB, Name:' Tyrannosaurus rex Coffee ', Image:'/1.jpg ', Price: -, Probability: -, CategoryId:' 552f76bd3e0b2dfca7989da3 ', __v:0, Status:1}, {_id:552F773a12831318a8d51ce4, Name:' Sword-toothed dragon Coffee ', Image:'/2.jpg ', Price: the, Probability: -, CategoryId:' 552f76bd3e0b2dfca7989da3 ', __v:0, Status:1}, {_id:552f776a87443f28a8e66931, Name:' Tyrannosaurus rex Coffee ', Image:'/3.jpg ', Price: the, Probability: -, CategoryId:' 552f76bd3e0b2dfca7989da3 ', __v:0, Status:1}, {_id:552f777f87443f28a8e66932, Name:' Tyrannosaurus rex Coffee ', Image:'/3.jpg ', Price: the, Probability: -, CategoryId:' 552f76bd3e0b2dfca7989da3 ', __v:0, Status:1}, {_id:552F77846A87B934A8A45CFA, Name:' winged Dragon Coffee ', Image:'/3.jpg ', Price: -, Probability: -, CategoryId:' 552f76bd3e0b2dfca7989da3 ', __v:0, Status:1}, {_id:552F779CAD597A3FA8365F63, Name:' Chicken Coffee ', Image:'/3.jpg ', Price: -, Probability: -, CategoryId:' 552f76bd3e0b2dfca7989da3 ', __v:0, Status:1}, {_id:552F77ac294f5d4ba8006b27, Name:' Jurassic Coffee ', Image:'/3.jpg ', Price: -, Probability: -, CategoryId:' 552f76bd3e0b2dfca7989da3 ', __v:0, Status:1} ] } ]

Mongoose Implement Dbref Find all sub-class information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.