NodeJSとMongoDBを学び始めました。私はMongoDBのからデータを取得したいが、問題を抱えていたでしょう:mongodbのクエリデータ
データ:今
{
"_id": ObjectId("58f6e6e09f800932f0285ea7"),
"id_user": "1",
"username": "admin",
"password": "123",
"area": {
"livingroom": {
"id_room" : "001",
"devices" : [
{
"id_device" : "1",
"device_name" : "Light 1",
"status" : "0",
"read-only" : "0"
},
{
"id_device" : "2",
"device_name" : "Light 2",
"status" : "0",
"read-only" : "1"
}
]
},
"garage": {
"id_room" : "002",
"devices" : [
{
"id_device" : "1",
"device_name" : "Light 1",
"status" : "0",
"read-only" : "0"
},
{
"id_device" : "2",
"device_name" : "Light 2",
"status" : "0",
"read-only" : "0"
}
]
},
}
}
、私はちょうどのみNodejsを使用することにより、 "リビングルーム" でデータを取得したい:
app.get('/devices', function (req, res) {
mongodb.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', url);
var collection = db.collection('Home');
collection.find({'area': 'livingroom'}).toArray(
function (err, result) {
if (err) {
console.log(err);
} else if (result.length) {
res.send(result);
} else {
console.log('No document(s) found with defined "find" criteria!');
}
db.close();
}
);
}
});
});
しかし、予想通りに動作しなかったため、何も受信できません。どうすれば解決できますか?
「期待どおりに動作しませんでした」とはどういう意味ですか?何かを出力していますか?あるいは、何かエラーが出ますか? –
あなたのデータの見た目にもよりますが、おそらく 'collection.find({'area.living_room':{$ ne:null}})' – Jenian
私は何も受け取りませんでした。申し訳ありません。 – ryan