0

all

Iam using Mean stack angularjs and mongodb now i have form like get the person information it's stored normal like table.my need is i have fields like this

Html code

<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>

    <script src="Controller/MongooseCrud.js"></script>
<title>Page Title</title>
</head>
<body>


    Name <input type="text" ng-model="user.name" > </br></br>
    Details</br></br>

    age:<input type="text"  ng-model="user.age"> </br></br>
     address:   <input type="text"  ng-model="user.address"> </br></br>
     Contact no:<input type="text" ng-model="user.number" > </br></br>

     <input type=button value=submit ng-click="Add()">

</body>
</html>

Controller Code

   $scope.Add = function () {
        $http.post('/AddNewuser', $scope.user).success(function (response) {
            refresh();
        });
    };

Server Code

app.post('/AddNewuser', function (req, res) {

    console.log(req.body);
    db.Users.insert(req.body, function (err, docs) {

        res.json(docs);
    });
});

it's store like table value's i want stored array inside the column

enter image description here

jose
  • 1,044
  • 1
  • 12
  • 35

1 Answers1

1

If you have no schema then you can try it

app.post('/AddNewuser', function (req, res) {

    console.log(req.body);
    var newUser = {
            name: req.body.name,
            details:{
                age: req.body.age,
                address: req.body.address,
                contactNo: req.body.contactNo
            }
       };
    db.Users.save(newUser , function (err, doc) {
        if(err) {
            console.log(err);
            return res.status(500).send('Error occurred');
        }
        return res.status(200).json(doc);
    });
});

N.B: Here I created details as a object instead of array

Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
  • Thanks.how to reterieve the Particular details values using GET @shaishab Roy – jose Mar 30 '17 at 11:38
  • db.users.find( function (err, docs) { console.log(docs); res.json(docs); }); – jose Mar 30 '17 at 11:40
  • `db.users.find(query, function (err, docs) { console.log(docs); res.json(docs); });` where query like `var query = {"details.age":24}` – Shaishab Roy Mar 30 '17 at 11:42
  • i have another doubt but its not relate to this question may i ask – jose Mar 30 '17 at 11:45
  • http://stackoverflow.com/questions/43134341/how-to-get-the-array-value-and-in-structurized-using-mongoose – jose Mar 31 '17 at 07:06
  • http://stackoverflow.com/questions/43139713/update-table-array-values-in-mongoose-or-mongojs – jose Mar 31 '17 at 11:42