如何按自定义顺序从3个猫鼬sachems检索数据

时间:2018-09-05 04:37:38

标签: node.js mongodb express mongoose

我在后端使用节点,表达,猫鼬。我创建了如下用户模式,

const mongoose = require('mongoose');

const addressSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    no: { type: String },
    firstStreet: { type: String },
    secondStreet: { type: String },
    city: { type: String, required: true },
    district: { type: String }
})

const contactDetailsSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    landNumber: { type: String },
    mobileNumber: { type: String },
    momNumber: { type: String },
    dadNumber: { type: String },
    gardianNumber: { type: String }
})

const userSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    email: { 
        type: String, 
        required: true, 
        unique:true,
        lowercase: true,
        match: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
    },
    password: { type: String, required: true },
    role: { type: String, required: true },

    fullName: { type: String, required: true },
    batch: { type: Number, required: true },
    subject: [{ 
    type: String
    }],
    school: { type: String, required: true },
    birthday: { type: String },
    address: { type: mongoose.Schema.Types.ObjectId, ref:'User' },
    contactDetails: { type: mongoose.Schema.Types.ObjectId, ref:'User' },
    stream: { type: String },
});

module.exports = {
    user: mongoose.model('User', userSchema),
    address: mongoose.model('Address', addressSchema),
    contactDetails: mongoose.model('ContactDetails', contactDetailsSchema)
};

,我创建了一个GET路由来检索所有用户的数据。 我将这些数据存储在具有相同_id的三个独立的mongoDB集合中。,我想获取所有用户数据。

我的用户GET路线是这样的,

const userModels = require('../models/user');

const User = userModels.user;
const Address = userModels.address;
const ContactDetails = userModels.contactDetails;

router.get('/', (req, res) =>{
User
    .find()
    .exec() 
    .then(docs => { 
        console.log(docs);
        const responce1 = {
            count: docs.length,
            Users: docs.map(doc => {
                return {
                    Message: 'User Details',
                    Id: doc._id,
                    Email: doc.email,
                    Role: doc.role,
                    Full_Name: doc.fullName,
                    Batch: doc.batch,
                    Subject: doc.subject,
                    School: doc.school,
                    Birthday: doc.birthday,
                    Stream: doc.stream,
                    request: {
                        type: 'get',
                        url: 'http://localhost:3000/user/' +doc._id
                    }
                }
            })
        }
        Address
            .find()
            .exec()
            .then(docs => {
                console.log(docs)
                responce2 = {
                    count: docs.length,
                    Address: docs.map(doc => {
                        return {
                            Message: 'Address',
                            _id: doc._id,
                            Address: doc.city,
                            First_Street: doc.firstStreet,
                            Second_Street: doc.secondStreet,
                            city: doc.city,
                            District: doc.district,
                        }
                    })
                }
            })
            .catch(err => {
                console.log(err);
                res.status(500).json({
                    error: err
                });
            });
        ContactDetails
            .find()
            .exec()
            .then(docs => {
                console.log(docs)
                responce3 = {
                    count: docs.length,
                    ContactDetails: docs.map(doc => {
                        return {
                            Message: 'Contact Details',
                            _id: doc._id,
                            Land_Number: doc.landNumber,
                            Mobile_Number: doc.mobileNumber,
                            Mom_Number: doc.momNumber,
                            Dad_Number: doc.dadNumber,
                            Gardian_Number: doc.gardianNumber,
                        }
                    })
                }
                res.status(200).json([responce1,responce2,responce3]);
            })
            .catch(err => {
                console.log(err);
                res.status(500).json({
                    error: err
                });
            });
    })
    .catch(err => {
        console.log(err);
        res.status(500).json({
            error: err
        });
    });
});

我得到这样的数据

[
{
    "count": 3,
    "Users": [
        {
            "Message": "User Details",
            "Id": "5b8e68fb9c898543b4970628",
            "Email": "new2@gmail.com",
            "Role": "student",
            "Full_Name": "Dinusha",
            "Batch": 16,
            "Subject": [
                "ICT"
            ],
            "School": "School",
            "Birthday": "1996/10/23",
            "Stream": "English",
            "request": {
                "type": "get",
                "url": "http://localhost:3000/user/5b8e68fb9c898543b4970628"
            }
        },
        {
            "Message": "User Details",
            "Id": "5b8e68fd9c898543b4970629",
            "Email": "new3@gmail.com",
            "Role": "student",
            "Full_Name": "Dinusha",
            "Batch": 16,
            "Subject": [
                "ICT"
            ],
            "School": "School",
            "Birthday": "1996/10/23",
            "Stream": "English",
            "request": {
                "type": "get",
                "url": "http://localhost:3000/user/5b8e68fd9c898543b4970629"
            }
        },
        {
            "Message": "User Details",
            "Id": "5b8f52e707c299266c0a6b97",
            "Email": "new4@gmail.com",
            "Role": "student",
            "Full_Name": "Dinusha",
            "Batch": 16,
            "Subject": [
                "ICT"
            ],
            "School": "School",
            "Birthday": "1996/10/23",
            "Stream": "English",
            "request": {
                "type": "get",
                "url": "http://localhost:3000/user/5b8f52e707c299266c0a6b97"
            }
        }
    ]
},
{
    "count": 3,
    "Address": [
        {
            "Message": "Address",
            "_id": "5b8e68fb9c898543b4970628",
            "Address": "city",
            "city": "city",
            "District": "rathnapura"
        },
        {
            "Message": "Address",
            "_id": "5b8e68fd9c898543b4970629",
            "Address": "city",
            "city": "city",
            "District": "rathnapura"
        },
        {
            "Message": "Address",
            "_id": "5b8f52e707c299266c0a6b97",
            "Address": "city",
            "city": "city",
            "District": "rathnapura"
        }
    ]
},
{
    "count": 3,
    "ContactDetails": [
        {
            "Message": "Contact Details",
            "_id": "5b8e68fb9c898543b4970628",
            "Land_Number": "072846",
            "Mobile_Number": "7368438",
            "Mom_Number": "7364738",
            "Dad_Number": "648364"
        },
        {
            "Message": "Contact Details",
            "_id": "5b8e68fd9c898543b4970629",
            "Land_Number": "072846",
            "Mobile_Number": "7368438",
            "Mom_Number": "7364738",
            "Dad_Number": "648364"
        },
        {
            "Message": "Contact Details",
            "_id": "5b8f52e707c299266c0a6b97",
            "Land_Number": "072846",
            "Mobile_Number": "7368438",
            "Mom_Number": "7364738",
            "Dad_Number": "648364"
        }
    ]
}
] 

但是我需要以这种形式获取每个用户的数据

[   
 "Users":
       {
            "Message": "User Details",
            "Id": "5b8e68fb9c898543b4970628",
            "Email": "new2@gmail.com",
            "Role": "student",
            "Full_Name": "Dinusha",
            "Batch": 16,
            "Subject": [
                "ICT"
            ],
            "School": "School",
            "Birthday": "1996/10/23",
            "Stream": "English",
            "request": {
                "type": "get",
                "url": "http://localhost:3000/user/5b8e68fb9c898543b4970628"
            }
        },
"Address": 
        {
            "Message": "Address",
            "_id": "5b8e68fb9c898543b4970628",
            "Address": "city",
            "city": "city",
            "District": "rathnapura"
        },
"ContactDetails": 
        {
            "Message": "Contact Details",
            "_id": "5b8e68fb9c898543b4970628",
            "Land_Number": "072846",
            "Mobile_Number": "7368438",
            "Mom_Number": "7364738",
            "Dad_Number": "648364"
        },
   ]

我可以得到那些数据吗? 如何从mongodb以自定义形式获取数据。

1 个答案:

答案 0 :(得分:1)