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);
    

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

  • 相关阅读:
    不常用的cmd命令
    js获取宽度
    Marshaling Data with Platform Invoke 概览
    Calling a DLL Function 之三 How to: Implement Callback Functions
    Marshaling Data with Platform Invoke 之四 Marshaling Arrays of Types
    Marshaling Data with Platform Invoke 之一 Platform Invoke Data Types
    Marshaling Data with Platform Invoke 之三 Marshaling Classes, Structures, and Unions(用时查阅)
    Calling a DLL Function 之二 Callback Functions
    WCF 引论
    Marshaling Data with Platform Invoke 之二 Marshaling Strings (用时查阅)
  • 原文地址:https://www.cnblogs.com/liziyou/p/6488758.html
Copyright © 2011-2022 走看看