Skip to content

Instantly share code, notes, and snippets.

@fgalan
Last active August 29, 2015 14:23
Show Gist options
  • Save fgalan/8fb78ea05d115c747e42 to your computer and use it in GitHub Desktop.
Save fgalan/8fb78ea05d115c747e42 to your computer and use it in GitHub Desktop.
Test FIWARE NGSIv2 query filters with MongoDB shell
# Test populating en1, en2, en3, en4, en5
// status equal to running -> en1, en2
> db.entities.find({"attrs.status.value": {$in: [ "running" ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
// status equal to running, error -> en1, en2, en4
> db.entities.find({"attrs.status.value": {$in: [ "running", "error" ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
// status unequal to running -> en3, en4, en5
> db.entities.find({"attrs.status.value": {$exists: true, $nin: [ "running" ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// status unequal to running, error -> en3, en5
> db.entities.find({"attrs.status.value": {$exists: true, $nin: [ "running", "error" ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// timestamp equal to 2017-06-17T07:21:24.238Z -> en2
> db.entities.find({"attrs.timestamp.value": {$in: [ new Date("2017-06-17T07:21:24.238Z") ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
// timestamp equal to 2017-06-17T07:21:24.238Z, 2017-06-17T07:22:43.112Z, 2017-06-17, -> en2, en4
> db.entities.find({"attrs.timestamp.value": {$in: [ new Date("2017-06-17T07:21:24.238Z"), new Date("2017-06-17T07:22:43.112Z"), new Date("2017-06-17,") ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
// timestamp unequal to 2017-06-17T07:21:24.238Z -> en1, en3, en4, en5
> db.entities.find({"attrs.timestamp.value": {$exists: true, $nin: [ new Date("2017-06-17T07:21:24.238Z") ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// timestamp unequal to 2017-06-17T07:21:24.238Z, 2017-06-17T07:10:12.328Z, 2017-06-17 -> en1, en3
> db.entities.find({"attrs.timestamp.value": {$exists: true, $nin: [ new Date("2017-06-17T07:21:24.238Z"), new Date("2017-06-17T07:10:12.328Z"), new Date("2017-06-17,") ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
// timestamp greater than 2017-06-17T07:20:00 -> en2, en3, en4
> db.entities.find({"attrs.timestamp.value": {$gt: new Date("2017-06-17T07:20:00") }}, {"_id.id": 1})
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
// timestamp lesser than 2017-06-17T07:20:00 -> en1, en5
> db.entities.find({"attrs.timestamp.value": {$lt: new Date("2017-06-17T07:20:00") }}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// timestamp greater than or equal 2017-06-17T07:21:24.238Z -> en2, en3, en4
> db.entities.find({"attrs.timestamp.value": {$gte: new Date("2017-06-17T07:21:24.238Z") }}, {"_id.id": 1})
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
// timestamp lesser than or equal 2017-06-17T07:21:24.238Z -> e1, en2, en5
> db.entities.find({"attrs.timestamp.value": {$lte: new Date("2017-06-17T07:21:24.238Z") }}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// timestamp inside range 2017-06-17T07:21:24.238Z to 2017-06-17T08:00:00 -> en1, en2
> db.entities.find({"attrs.timestamp.value": {$gte: new Date("2017-06-17T07:21:24.238Z"), $lte: new Date("2017-06-17T08:00:00") }}, {"_id.id": 1})
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
// timestamp outside range 2017-06-17T07:12:00 to 2017-06-17T07:21:24.238Z -> en3, en4
> db.entities.find({"attrs.timestamp.value": {$exists: true, $not: {$gte: new Date("2017-06-17T07:00:00"), $lte: new Date("2017-06-17T07:21:24.238Z") }}}, {"_id.id": 1})
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
// temperature equal to 27 -> en2
> db.entities.find({"attrs.temperature.value": {$in: [ 27 ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
// temperature equal to 31, 17.8, 22 -> en3, en4
> db.entities.find({"attrs.temperature.value": {$in: [ 31, 17.8, 22 ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
// temperature unequal to 31 -> en1, en2, en4, en5
> db.entities.find({"attrs.temperature.value": {$exists: true, $nin: [ 31 ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// temperature unequal to 24, 26.5, 28 -> en2, en3, en4
> db.entities.find({"attrs.temperature.value": {$exists: true, $nin: [ 24, 26.5, 28 ]}}, {"_id.id": 1})
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
// temperature greater than 26 -> en1, en2, en3
> db.entities.find({"attrs.temperature.value": {$gt: 26 }}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
// temperature lesser than 27 -> en1, en4, en5
> db.entities.find({"attrs.temperature.value": {$lt: 27 }}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// temperature greater than or equal 27 -> en2, en3
> db.entities.find({"attrs.temperature.value": {$gte: 27 }}, {"_id.id": 1})
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
// temparature lesser than or equal 24 -> en4, en5
> db.entities.find({"attrs.temperature.value": {$lte: 24 }}, {"_id.id": 1})
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// temperature inside range 17 to 24 -> en4, en5
> db.entities.find({"attrs.temperature.value": {$gte: 17, $lte: 24 }}, {"_id.id": 1})
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// temperature outside range 17 to 24 -> en1, en2, en3
>db.entities.find({"attrs.temperature.value": {$exists: true, $not: {$gte: 17, $lte: 24 }}}, {"_id.id": 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
# Test populating en1, en2, en3, en4, en5, en6, en7
// entities with status attribute (+status) -> en1, en2, en3, en4, en5
> db.entities.find({"attrs.status": {$exists: true}}, {_id: 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
> db.entities.find({"attrNames": { $in: [ "status" ] }}, {_id: 1})
{ "_id" : { "id" : "en1", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en2", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en3", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en4", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en5", "type" : "device", "servicePath" : "/" } }
// entities without status attribute (-status) -> en6, en7
> db.entities.find({"attrs.status": {$exists: false}}, {_id: 1})
{ "_id" : { "id" : "en6", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en7", "type" : "", "servicePath" : "/" } }
> db.entities.find({"attrNames": { $nin: [ "status" ] }}, {_id: 1})
{ "_id" : { "id" : "en6", "type" : "device", "servicePath" : "/" } }
{ "_id" : { "id" : "en7", "type" : "", "servicePath" : "/" } }
// entities without entity type (-type) -> en7
> db.entities.find({"_id.type": ""}, {_id: 1})
{ "_id" : { "id" : "en7", "type" : "", "servicePath" : "/" } }
// run: mongo localhost:27017/orion populate1to5.js
template = {
"_id" : {
"id" : null,
"type" : null,
"servicePath" : "/"
},
"attrNames" : [
"temperature",
"timestamp",
"status"
],
"attrs" : {
"temperature" : {
"type" : "number",
"creDate" : 1434525527,
"modDate" : 1434525527,
"value" : null
},
"timestamp" : {
"type" : "number",
"creDate" : 1434525527,
"modDate" : 1434525527,
"value" : null
},
"status" : {
"type" : "number",
"creDate" : 1434525527,
"modDate" : 1434525527,
"value" : null
}
},
"creDate" : 1434525527,
"modDate" : 1434525527
}
doc1 = JSON.parse(JSON.stringify(template))
doc2 = JSON.parse(JSON.stringify(template))
doc3 = JSON.parse(JSON.stringify(template))
doc4 = JSON.parse(JSON.stringify(template))
doc5 = JSON.parse(JSON.stringify(template))
doc1._id.id = "en1"
doc1._id.type = "device"
doc1.attrs.temperature.value = 26.5
doc1.attrs.timestamp.value = ISODate("2017-06-17T07:12:25.823Z")
doc1.attrs.status.value = "running"
doc2._id.id = "en2"
doc2._id.type = "device"
doc2.attrs.temperature.value = 27
doc2.attrs.timestamp.value = ISODate("2017-06-17T07:21:24.238Z")
doc2.attrs.status.value = "running"
doc3._id.id = "en3"
doc3._id.type = "device"
doc3.attrs.temperature.value = 31
doc3.attrs.timestamp.value = ISODate("2017-06-17T08:19:12.231Z")
doc3.attrs.status.value = "shutdown"
doc4._id.id = "en4"
doc4._id.type = "device"
doc4.attrs.temperature.value = 17.8
doc4.attrs.timestamp.value = ISODate("2017-06-17T07:22:43.112Z")
doc4.attrs.status.value = "error"
doc5._id.id = "en5"
doc5._id.type = "device"
doc5.attrs.temperature.value = 24
doc5.attrs.timestamp.value = ISODate("2017-06-17T07:10:12.328Z")
doc5.attrs.status.value = "shutdown"
db.entities.insert(doc1)
db.entities.insert(doc2)
db.entities.insert(doc3)
db.entities.insert(doc4)
db.entities.insert(doc5)
// run: mongo localhost:27017/orion populate6to7.js
doc6 = {
"_id" : {
"id" : "en6",
"type" : "device",
"servicePath" : "/"
},
"attrNames" : [
"temperature",
"timestamp"
],
"attrs" : {
"temperature" : {
"type" : "number",
"creDate" : 1434525527,
"modDate" : 1434525527,
"value" : 45
},
"timestamp" : {
"type" : "number",
"creDate" : 1434525527,
"modDate" : 1434525527,
"value" : ISODate("2017-06-17T07:21:24.238Z")
}
},
"creDate" : 1434525527,
"modDate" : 1434525527
}
db.entities.insert(doc6)
doc7 = {
"_id" : {
"id" : "en7",
"type" : "",
"servicePath" : "/"
},
"attrNames" : [
"temperature",
"timestamp"
],
"attrs" : {
"temperature" : {
"type" : "number",
"creDate" : 1434525527,
"modDate" : 1434525527,
"value" : 45
},
"timestamp" : {
"type" : "number",
"creDate" : 1434525527,
"modDate" : 1434525527,
"value" : ISODate("2017-06-17T07:21:24.238Z")
}
},
"creDate" : 1434525527,
"modDate" : 1434525527
}
db.entities.insert(doc7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment