实际上我想在搜索特定的服务器场数组后在服务器场数组中添加新设备(使用_id搜索它)。如何使用mongoose在node js中实现此操作?我在插入数据时遇到困难吗?猫鼬的新手。请帮忙。
我的代码产生
//addDevice
//map to required farm in which i want to add device
router.put("addDevice/:id/",(req,res) =>{
const deviceName=req.body.deviceName;
const deviceType=req.body.deviceType;
const description=req.body.description;
const deviceLocation=req.body.deviceLocation;
const Parameter=req.body.Parameter;
const Topic=("v1/"+farmName+"/"+deviceName+"/"+deviceLocation+"/"+Parameter);
User.findByIdAndUpdate(req.params.id,
{$push:{"farm.0.device":{farm,deviceName,deviceType,description,deviceLocation,Parameter,Topic}},$currentDate:{lastModified:true,"deviceModifyDate":{$type:"timestamp"}}},
{safe:true,upsert:true,multi:false,new:true},
function(err,User){
if(err)
{
console.log(err);
return res.send(err);
}
return res.status(200).json(User);
});
});
{
"_id": "5d3ac84a86688123789e13b2",
"name": "Puja",
"email": "puja@gmail.com",
"password": "$2a$10$r17zOU1gTJvwoB.nBdRvi.qvDmsJhIlHbp//s8l0KEKgCEvbKoZAu",
"location": "Delhi",
"createdDate": "2019-07-26T09:30:50.243Z",
"farm": [
{
"location": [
"11.33,22.99"
],
"farmAddDate": "2019-07-30T08:03:34.247Z",
"device": [
{
"_id": "5d402def7cef9f20c8272fc4",
"deviceName": " DeviceX01",
"deviceType": " Test",
"description": " This is a test",
"deviceLocation": "11.22,33.12",
"Parameter": " temp",
"Topic": "v1/ DeviceX01/11.22,33.12/ temp"
},
{
"_id": "5d42b664243d02054417e2ab",
"deviceName": " Test 001",
"deviceType": " 1",
"description": " 2",
"deviceLocation": "3",
"Parameter": " temp",
"Topic": "v1/ Test 001/3/ temp"
},
{
"_id": "5d42b8208c13262db052454f",
"deviceName": " ",
"deviceType": " ",
"description": " ",
"deviceLocation": [],
"Parameter": " ",
"Topic": "v1/ // "
},
{
"_id": "5d42b8258c13262db0524550",
"deviceName": " ",
"deviceType": " ",
"description": " ",
"deviceLocation": [],
"Parameter": " ",
"Topic": "v1/ // "
},
{
"_id": "5d440bd687241b23341ce2ab",
"deviceName": "Xkito",
"deviceType": "Musiic",
"description": "This is test for updation of specific data in a specfic farm",
"deviceLocation": [
"11",
"85.33"
],
"Parameter": "Hello",
"Topic": "v1//Xkito/11,85.33/Hello"
},
{
"_id": "5d440bf50a3bbb2c9417bb53",
"deviceName": "Xkito",
"deviceType": "Musiic",
"description": "This is test for updation of specific data in a specfic farm",
"deviceLocation": [
"11",
"85.33"
],
"Parameter": "Hello",
"Topic": "v1//Xkito/11,85.33/Hello"
},
{
"_id": "5d441348f1877637cc712d04",
"deviceName": "Xkito",
"deviceType": "Musiic",
"description": "This is test for updation of specific data in a specfic farm",
"deviceLocation": [
"11",
"85.33"
],
"Parameter": "Hello",
"Topic": "v1/Xkito/11,85.33/Hello"
}
],
"_id": "5d3ff9d68e34cb265c030819",
"farmName": "Durgapur",
"description": "Testing that info is saved "
},
{
"location": [],
"farmAddDate": "2019-08-01T11:48:01.883Z",
"device": [
{
"_id": "5d441348f1877637cc712d04",
"deviceName": "Xkito",
"deviceType": "Musiic",
"description": "This is test for updation of specific data in a specfic farm",
"deviceLocation": [
"11",
"85.33"
],
"Parameter": "Hello",
"Topic": "v1/Xkito/11,85.33/Hello"
}
],
"_id": "5d42d171e7ceef2a90245470",
"farmName": "Xilo",
"description": "This is test from Postman Api Request"
}
],
"__v": 0,
"farmModifyDate": "6720163877251710977",
"lastModified": "2019-08-02T10:41:12.141Z",
"deviceModifyDate": "6720517743902195713"
}
我希望设备可以根据要求添加到特定服务器场中。