使用mongojs通过objectid从mongodb获取条目

时间:2022-11-15 03:04:07

i created an web application with node.js and mongo.js where i can type in data into an html page and send it so my mongodb databse. now i want to type in the objectid in my html page and get back my entry. this is my html code:

我用node.js和mongo.js创建了一个web应用程序,在那里我可以将数据输入到html页面并将其发送给我的mongodb数据库。现在我想在我的html页面输入objectid并取回我的条目。这是我的HTML代码:

<div class="fetch" ng-controller="AppCtrl">
    <center><b>ID:&nbsp;</b><input class="form-fetch" ng-model="patient._id">&nbsp;&nbsp;<button class="btn btn-primary" ng-click="fetchPatient(patient._id)">Fetch Patient</button></center>
</div>

this is my controller.js:

这是我的controller.js:

    $scope.fetchPatient = function (id) {
    console.log($scope.patient._id);
    $http.get('/patientlist/:id').success(function (response) {
    });
    }

and this is my server.js:

这是我的server.js:

app.get('/patientlist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
db.patientlist.findOne({ _id: mongojs.ObjectId(id) }, function (err, doc) {
    res.json(doc);
});
});

1 个解决方案

#1


0  

I guess the _id is created by you so don't put ObjectId around it:

我想_id是由你创建的,所以不要把ObjectId放在它周围:

db.patientlist.findOne({ _id: id }, function (err, doc) {
    res.json(doc);
});

ObjectId is for the _id auto-generated by MongoDB.

ObjectId用于MongoDB自动生成的_id。

#1


0  

I guess the _id is created by you so don't put ObjectId around it:

我想_id是由你创建的,所以不要把ObjectId放在它周围:

db.patientlist.findOne({ _id: id }, function (err, doc) {
    res.json(doc);
});

ObjectId is for the _id auto-generated by MongoDB.

ObjectId用于MongoDB自动生成的_id。