zoukankan      html  css  js  c++  java
  • mongoDB内置文档定义

      在最近的设计数据库时,犯了一个低级的错误,就是设置内置文档是定义了错误了,导致数据取不出,去找了很多资料都无法解决。最后看了一了一下自己设置的model文件。配置错误,所以导致数据取不出了。

    数据库时这样设计的

    var mongoose = require('mongoose'),
        Schema = mongoose.Schema;
    
    //realTimeloginSchema 停车场运行实时监控
    var realTimeloginSchema = new Schema({
        realuserId: {type: String},    //用户ID
        realPwd: {type: String},       //用户密码
        realAddTime: {type: Date},     //注册时间
        realLoginTime: {type: Date},   //登录时间
        realRoot:{type: Boolean,default:false},  //是否是超级用户,默认是false
        realPower: {type: Boolean,default:false}, //添加用户权限,默认是false
       realpark:{
            parkId:{type:String},   //停车场名称
            parkName:{type:String}, //停车场ID
            scale: {type: Number},  //停车场总车位
             pionts:{  lng :{type:String},        //经度
                        lat :{type:String}        //纬度
                     }
        }
      }
    );
    
    module.exports = mongoose.model("realTimelogin ", realTimeloginSchema);
    

    自己在可视化工具robomongo是这样数据是这样的:

    {
        "_id" : ObjectId("58afe978c0120986cddacf6c"),
        "realPwd" : "124",
        "realuserId" : "124",
        "realAddTime" : ISODate("2017-02-26T10:05:51.643Z"),
        "realPower" : true,
        "realRoot" : false,
        "realpark" : [ 
            {
                "parkId" : "99999888881474167822",
                "parkName" : "小猫停车场",
                "scale" : 200
            }, 
            {
                "parkId" : "0755000021433988491",
                "parkName" : "软件产业基地停车场",
                "scale" : 500
            }, 
            {
                "parkId" : "18682397901474189348",
                "parkName" : "新版测试停车场",
                "scale" : 1250
            }
        ]
    }
    

      console.log(doc.realpark);是可以打印数据的,但是一直取不到realpark的长度,所以无法遍历对象。

    之后查看了一下官网文档,才知道自己定义错误,应该这样定义:

    var mongoose = require('mongoose'),
        Schema = mongoose.Schema;
    
    //realTimeloginSchema 停车场运行实时监控
    var realTimeloginSchema = new Schema({
        realuserId: {type: String},    //用户ID
        realPwd: {type: String},       //用户密码
        realAddTime: {type: Date},     //注册时间
        realLoginTime: {type: Date},   //登录时间
        realRoot:{type: Boolean,default:false},  //是否是超级用户,默认是false
        realPower: {type: Boolean,default:false}, //添加用户权限,默认是false
        realpark: {type: Object, default: []}
       /* realpark:{
            parkId:{type:String},   //停车场名称
            parkName:{type:String}, //停车场ID
            scale: {type: Number},  //停车场总车位
             pionts:{  lng :{type:String},        //经度
                        lat :{type:String}        //纬度
                     }
    
        }
        */
      }
    );
    
    module.exports = mongoose.model("realTimelogin ", realTimeloginSchema);
    

      这样就可以得到内嵌对象的长度,遍历对象。

  • 相关阅读:
    SURF与SIFT
    CVMAT操作
    flask
    爬虫
    mysql基础
    WINCE6.0_CHS_SDK安装失败
    com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1443 连接到主机 localhost 的 TCP/IP 连接失败。
    office 2010每次打开word都要重新配置的解决方法
    如何使用Visual Studio 2008编译C语言
    Hibernate初学、遇到的问题
  • 原文地址:https://www.cnblogs.com/liziyou/p/6488758.html
Copyright © 2011-2022 走看看