- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在尝试使用 mongoose 和 mongodb 在 Node.js 中发出 get 请求。
我的用户有一个包含设备数组对象的农场数组对象列表。我正在查询并尝试获取特定设备的字段。
这里我尝试使用聚合函数,但它返回空数组。
后端代码
router.get("/getParameter/:farmID/:deviceID",async(req,res)=>{
User.aggregate([
{$match:{"farm._id":req.params.farmID}},
{$unwind:'$farm'},
{$match:{"device._id":req.params.deviceID}},
{$unwind:'$device'},
{$match:{"device.Parameter":{$exists:true}}},
{$project:{"Parameter":1}}
]).exec((err,result)=>{
if (err) throw err
console.log(result)
res.send(result)
});
})
用户架构
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
mongoose.set('useFindAndModify', false);
//Device Schema
const Device = new Schema({
deviceName: {
type: String,
required: true,
unique:true
},
deviceType: {
type: String,
required: true
},
description:{
type:String,
required:false
},
deviceLocation:{
default:Array,
"default":[],
required:false
},
Parameter:{
type:String
},
Topic:{
type:Array,
"default":[]
},
AddedDate: {
type: Date,
default: Date.now
},
});
// User Schema
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true
},
location:{
type:String,
required:true,
},
createdDate: {
type: Date,
default: Date.now
},
farm:[
{ farmName:{type:String,index:true},
description:String,
location:{type:Array,"default":[]},
farmAddDate:{type:Date,default:Date.now},
device:[{Device}]
},
],
},
{strict:false}
);
module.exports = User = mongoose.model("users", UserSchema);
//Location Co-Ordiantes Schema
// const pointSchema = new Schema({
// type: {
// type: String,
// default:"Point"
// },
// coordinates: {
// type: [Number],
// }
// });
示例文档:
[
{
"_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"
],
"_id": "5d3ff9d68e34cb265c030819",
"farmName": "Xyz",
"description": "Testing that info is saved ",
"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"
}
]
},
{
"location": [],
"_id": "5d42d171e7ceef2a90245470",
"farmName": "Xilo",
"description": "This is test from Postman Api Request",
"farmAddDate": "2019-08-01T11:48:01.883Z",
"device": [
{
"_id": "5d441878f1877637cc712d07",
"deviceName": "Xki",
"deviceType": "Muc",
"description": "This is test for updation of specific data in a specfic farm .Test Pass",
"deviceLocation": [
"11",
"180"
],
"Parameter": "Hello Happy",
"Topic": "v1/Xki/11,180/Hello Happy"
},
{
"_id": "5d4418953968370e64c32e1f",
"deviceName": "Xki",
"deviceType": "Muc",
"description": "This is test for updation of specific data in a specfic farm .Test Pass",
"deviceLocation": [
"11",
"180"
],
"Parameter": "Hello Happy",
"Topic": "v1/Xki/11,180/Hello Happy"
},
{
"_id": "5d4425548b3cf92af46e2c9a",
"deviceName": "Xki",
"deviceType": "Muc",
"description": "This is test for updation of specific data in a specfic farm .Test Pass",
"deviceLocation": [
"11",
"180"
],
"Parameter": "Hello Happy 1",
"Topic": "sagita/5d42d171e7ceef2a90245470/Xki/11,180/Hello Happy 1"
},
{
"_id": "5d44256d8b3cf92af46e2c9b",
"deviceName": " DeviceX01",
"deviceType": " Test",
"description": " This is a test",
"deviceLocation": "11.22,33.12",
"Parameter": " set",
"Topic": "sagita/5d42d171e7ceef2a90245470/ DeviceX01/11.22,33.12/ set",
"DeviceJson": [
"{ \"$schema\": \"http: //json-schema.org/draft-04/schema#\", \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"name\": { \"type\": \"string\" }, \"email\": { \"type\": \"string\" }, \"password\": { \"type\": \"string\" }, \"location\": { \"type\": \"string\" }, \"createdDate\": { \"type\": \"Date\" }, \"farm\": { \"type\": \"array\", \"items\": [ { \"type\": \"object\", \"properties\": { \"location\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" } ] }, \"_id\": { \"type\": \"ObjectId\" }, \"farmName\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"farmAddDate\": { \"type\": \"Date\" }, \"device\": { \"type\": \"array\", \"items\": [ { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"string\" }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] } ] } }, \"required\": [ \"location\", \"_id\", \"farmName\", \"description\", \"farmAddDate\", \"device\" ] }, { \"type\": \"object\", \"properties\": { \"location\": { \"type\": \"array\", \"items\": {} }, \"_id\": { \"type\": \"ObjectId\" }, \"farmName\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"farmAddDate\": { \"type\": \"Date\" }, \"device\": { \"type\": \"array\", \"items\": [ { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" }, { \"type\": \"string\" } ] }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] }, { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObejctId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" }, { \"type\": \"string\" } ] }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] }, { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" }, { \"type\": \"string\" } ] }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] }, { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"string\" }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" }, \"DeviceJson\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" } ] } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\", \"DeviceJson\" ] } ] } }, \"required\": [ \"location\", \"_id\", \"farmName\", \"description\", \"farmAddDate\", \"device\" ] }, { \"type\": \"object\", \"properties\": { \"location\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" } ] }, \"_id\": { \"type\": \"ObjectId\" }, \"farmName\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"farmAddDate\": { \"type\": \"Date\" }, \"device\": { \"type\": \"array\", \"items\": [ { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" }, { \"type\": \"string\" } ] }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] } ] } }, \"required\": [ \"location\", \"_id\", \"farmName\", \"description\", \"farmAddDate\", \"device\" ] } ] }, \"__v\": { \"type\": \"integer\" }, \"farmModifyDate\": { \"type\": \"Timestamp\" }, \"lastModified\": { \"type\": \"Timestamp\" }, \"deviceModifyDate\": { \"type\": \"Timestamp\" }, \"deviceModified\": { \"type\": \"Timestamp\" } }, \"required\": [ \"_id\", \"name\", \"email\", \"password\", \"location\", \"createdDate\", \"farm\", \"__v\", \"farmModifyDate\", \"lastModified\", \"deviceModifyDate\", \"deviceModified\" ] }"
]
}
]
},
{
"location": [
"11.22,65.33"
],
"_id": "5d492a4a1294110f64196b2d",
"farmName": "Durgapur",
"description": "This is a test for frontend to backend",
"farmAddDate": "2019-08-06T07:20:42.654Z",
"device": [
{
"_id": "5d4bd62900b433276c2845af",
"deviceName": "Farm @2 Device 001",
"deviceType": "Intel Edision",
"description": "This is a test for creation of Data Bucket of Topic",
"deviceLocation": [
"12.33",
"56.33"
],
"Parameter": "Temperature",
"Topic": "sagita/model/5d492a4a1294110f64196b2d/Farm @2 Device 001/12.33,56.33/Temperature"
}
]
}
],
"__v": 0,
"lastModified": "2019-12-04T06:59:17.431Z",
"farmModifyDate": "6764284414131175425",
"deviceModifyDate": "6720517743902195713",
"deviceModified": "6766475118035075073"
}
]
是否有其他方法可以通过传递 farm._id 和 device._id 来获取参数字段?
最佳答案
您需要更改第二个展开阶段和第二个匹配阶段的顺序,并在匹配字段中使用正确的字段名称。
[
{
$match: {
"farm._id": req.params.farmID
}
},
{
$unwind: "$farm"
},
{
$unwind: "$farm.device"
},
{
$match: {
"farm.device._id": req.params.deviceID
}
},
{
$project: {
"parameter": "$farm.device.Parameter"
}
}
])
我也会这样写我的路线:
router.get("/getParameter/:farmID/:deviceID", async (req, res) => {
try {
const result = await User.aggregate([
{
$match: {
"farm._id": req.params.farmID
}
},
{
$unwind: "$farm"
},
{
$unwind: "$farm.device"
},
{
$match: {
"farm.device._id": req.params.deviceID
}
},
{
$project: {
parameter: "$farm.device.Parameter"
}
}
]);
if (result.length > 0) {
res.send(result[0].parameter); //or you can return the object itself: res.send(result[0])
} else {
res.status(400).send("No value found, check your request values");
}
} catch (err) {
console.log(err);
res.status(500).send("Something went wrong");
}
});
关于node.js - collection.Aggregate 不适用于具有另一个对象数组的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390136/
是否有带有索引的.collect?我想做这样的事情: def myList = [ [position: 0, name: 'Bob'], [position: 0, name: 'J
我创建了一个 Collection 类,它扩展了 ArrayList 以添加一些有用的方法。它看起来像这样: public class Collection extends ArrayList {
我知道如果我有元素,我想得到 List/Set/Map 我可以调用这个元素: Collections.singleton()/Collections.singletonList()/Collectio
我刚刚在我的 pom 文件中看到 Apache commons-collections 有两个不同的组 ID: commons-collections commons-collect
我们可以对所有 Collections 类型的对象(如 Set 和 List)使用 Collections.synchronizedCollection(Collection c),这就是为什么我们有
我有List>我想让它把上一个集合中的所有人复制到List收藏。 我是这样做的: var People = new List>{ new List{...},... };
我想做的是使用良好的旧循环非常简单。 假设我有一个包含 B 列表的对象 A。 public class A { public List myListOfB; } 在其他一些方法中,我有一个 As
在 Capgemini 的采访中,我被问到一个我无法回答的问题。所有集合类和接口(interface)共有的那些方法是什么? 最佳答案 所有 java 对象类(包括所有集合)都派生自名为 Object
我有一系列存储估计信息的数据库表。当设置某些边界时,我试图从所有数据库表中返回所有数据。 收藏 $estimateItems = new Collection(); $esti
为什么 Haskell 实现如此专注于链表? 例如,我知道 Data.Sequence 效率更高 大多数列表操作(cons 操作除外),并且被大量使用; 但是,从语法上讲,它“几乎不受支持”。 Has
我试图简单地将我在 PHP 中请求的内容返回到 JSON。我的问题是每个库存尚未完成。事实上,它是“渲染”,但“this.collection.models”尚未完成,因为请求尚未完成。 我应该如何解
本质上,作为Powershell脚本的一部分,我需要实现广度优先搜索。因此,我需要队列,并且认为System.Collections.Queue与其他任何队列一样好。但是,当我从队列中取出一个对象时,
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是 on-topic用于堆栈溢出。 已关闭10 年前。 Improve t
嗨,我不明白为什么这不起作用? Notifications.update({'userId':Meteor.userId(), 'notifyUserId':notifyFriendId}, {$se
假设我有一个闭包: def increment = {value, step -> value + step } 现在我想遍历我的整数集合的每个项目,用 5 递增,并将新元素保存到一个新集合中:
使用逐页 View 时,我的 plone 集合文件夹未显示所有项目。基本上我有 9 页包含元素,但第 6 - 8 页显示的内容完全相同。因此,并非所有项目都会显示,即使项目总数对应于应该在集合中的元素
private Map> map ,其中 ProgramCourse 是我的项目中的域类,上面的 map 是我运行项目时域类 Program 的字段以下异常即将到来。 Use of @OneToMan
三者的主要区别是什么?现在,我想分别使用字符串/字符串创建一个键/值对。这三个似乎都有我可以使用的选项。 编辑:我只想创建一个简单的哈希表 - 没什么特别复杂的。 最佳答案 通用集合几乎完全取代了基础
我正在为 NodeJs 使用 mongodb 驱动程序,其中有 3 个方法: 1) db.collection.insert 2) 数据库.collection.insertOne 3) db.col
我有一个集合,我正在尝试使用 Distinct 方法删除重复项。 public static Collection imagePlaylist imagePlaylist = imagePlaylis
我是一名优秀的程序员,十分优秀!