最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何使用mongoose和Node.js只检索Azure Cosmos中的子对象?

IT培训 admin 3浏览 0评论

如何使用mongoose和Node.js只检索Azure Cosmos中的子对象?

我使用Azure cosmos db和Mongodb API。我也使用mongoose来创建模式并在数据库中创建新文档。我也在使用Node.js.

此时我正在考虑使用与嵌入式文档的一对多关系。

数据结构如下:

{
    "_id" : "locality1",
    "_type" : "Locality",
    "name" : "Wallmart",
    "subsectionList" : [
        {
            "_id" : "subsection1",
            "_type" : "SubSection",
            "name" : "First floor",
            "sensorList" : [
                {
                    "_id" : "sensor1",
                            "_type" : "Sensor",
                    "placement" : "In the hallway"
                },
                {
                    "_id" : "sensor2",
                            "_type" : "Sensor",
                    "placement" : "In the ceiling"
                }
            ]
        },
        {
            "_id" : "subsection2",
            "_type" : "SubSection",
            "name" : "Second floor",
            "sensorList" : [ ],
        }
    ],
}

我想只检索“sensor1”对象,而不是父对象的任何东西。

使用查询我只能检索整个“locality1”对象及其所有底层子部分和传感器。在更大的范围内,这是一个不必要的大量数据。

这是我到目前为止的查询。

Locality.find().where('subsectionList.sensorList._id').equals("sensor1").then(doc => {
    console.log(doc)
  })

我很感激任何提示! :)

回答如下:

根据我的测试,我无论如何都无法摆脱_id属性,即使我遵循here提到的参数。

Locality.find({},'subsectionList', function (err, locas) 

上面的查询仍然返回包括_id属性的结果。(这似乎是一个默认项)

我从这个blog得到一个解决方法,你可以循环数组来过滤你想要的列。

var mongoose = require('mongoose');
var COSMOSDB_CONNSTR= "mongodb://***.documents.azure:10255/db";
var COSMODDB_USER= "***";
var COSMOSDB_PASSWORD= "***";

mongoose.connect(COSMOSDB_CONNSTR+"?ssl=true&replicaSet=globaldb", {
  auth: {
    user: COSMODDB_USER,
    password: COSMOSDB_PASSWORD
  }
}).then(() => console.log('Connection to CosmosDB successful'))
.catch((err) => console.error(err));

const Locality = mongoose.model('Locality', new mongoose.Schema({
    _id: String,
    subsectionList: [{
        sensorList: [{
            _id: String,
            _type: String,
            placement: String
        }]
    }]
}));

Locality.find({},'subsectionList', function (err, locas) {
  if (err) return handleError(err);

  var returnArray = [];
  for(var i = 0; i<locas.length;i++){
      for(var j = 0; j<locas[i].subsectionList.length;j++){
          for(var k = 0; k<locas[i].subsectionList[j].sensorList.length;k++){
           if(locas[i].subsectionList[j].sensorList[k]._id == 'sensor1')
             returnArray.push(locas[i].subsectionList[j].sensorList[k]);
          }
      }
  }
  console.log(returnArray);
});

如何使用mongoose和Node.js只检索Azure Cosmos中的子对象?

我使用Azure cosmos db和Mongodb API。我也使用mongoose来创建模式并在数据库中创建新文档。我也在使用Node.js.

此时我正在考虑使用与嵌入式文档的一对多关系。

数据结构如下:

{
    "_id" : "locality1",
    "_type" : "Locality",
    "name" : "Wallmart",
    "subsectionList" : [
        {
            "_id" : "subsection1",
            "_type" : "SubSection",
            "name" : "First floor",
            "sensorList" : [
                {
                    "_id" : "sensor1",
                            "_type" : "Sensor",
                    "placement" : "In the hallway"
                },
                {
                    "_id" : "sensor2",
                            "_type" : "Sensor",
                    "placement" : "In the ceiling"
                }
            ]
        },
        {
            "_id" : "subsection2",
            "_type" : "SubSection",
            "name" : "Second floor",
            "sensorList" : [ ],
        }
    ],
}

我想只检索“sensor1”对象,而不是父对象的任何东西。

使用查询我只能检索整个“locality1”对象及其所有底层子部分和传感器。在更大的范围内,这是一个不必要的大量数据。

这是我到目前为止的查询。

Locality.find().where('subsectionList.sensorList._id').equals("sensor1").then(doc => {
    console.log(doc)
  })

我很感激任何提示! :)

回答如下:

根据我的测试,我无论如何都无法摆脱_id属性,即使我遵循here提到的参数。

Locality.find({},'subsectionList', function (err, locas) 

上面的查询仍然返回包括_id属性的结果。(这似乎是一个默认项)

我从这个blog得到一个解决方法,你可以循环数组来过滤你想要的列。

var mongoose = require('mongoose');
var COSMOSDB_CONNSTR= "mongodb://***.documents.azure:10255/db";
var COSMODDB_USER= "***";
var COSMOSDB_PASSWORD= "***";

mongoose.connect(COSMOSDB_CONNSTR+"?ssl=true&replicaSet=globaldb", {
  auth: {
    user: COSMODDB_USER,
    password: COSMOSDB_PASSWORD
  }
}).then(() => console.log('Connection to CosmosDB successful'))
.catch((err) => console.error(err));

const Locality = mongoose.model('Locality', new mongoose.Schema({
    _id: String,
    subsectionList: [{
        sensorList: [{
            _id: String,
            _type: String,
            placement: String
        }]
    }]
}));

Locality.find({},'subsectionList', function (err, locas) {
  if (err) return handleError(err);

  var returnArray = [];
  for(var i = 0; i<locas.length;i++){
      for(var j = 0; j<locas[i].subsectionList.length;j++){
          for(var k = 0; k<locas[i].subsectionList[j].sensorList.length;k++){
           if(locas[i].subsectionList[j].sensorList[k]._id == 'sensor1')
             returnArray.push(locas[i].subsectionList[j].sensorList[k]);
          }
      }
  }
  console.log(returnArray);
});

发布评论

评论列表 (0)

  1. 暂无评论