如何在Mongoose模式中正确定义带有2D地理索引的数组对象

174
我目前在创建以下文档的模式时遇到了问题。服务器的响应总是将“trk”字段值返回为[Object]。不知道该怎么做,因为我尝试了至少对我有意义的所有方法;-)
如果有帮助的话,我的Mongoose版本是3.6.20和MongoDB 2.4.7 还有,在我忘记之前,将其设置为索引(2d)也会很好。
原始数据:
{
    "_id": ObjectId("51ec4ac3eb7f7c701b000000"),
    "gpx": {
        "metadata": {
            "desc": "Nürburgring VLN-Variante",
            "country": "de",
            "isActive": true
        },
    "trk": [
    {
        "lat": 50.3299594,
        "lng": 6.9393006
    },
    {
        "lat": 50.3295046,
        "lng": 6.9390688
    },
    {
        "lat": 50.3293714,
        "lng": 6.9389939
    },
    {
        "lat": 50.3293284,
        "lng": 6.9389634
    }]
    }
}

Mongoose模式:

var TrackSchema = Schema({
            _id: Schema.ObjectId,
            gpx: {
                metadata: {
                    desc: String,
                    country: String,
                    isActive: Boolean
                },
                trk: [{lat:Number, lng:Number}]
            }
        }, { collection: "tracks" });

在Chrome的网络选项卡中,响应始终看起来像这样(这只是错误的trk部分):

{ trk: 
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],

我已经尝试了不同的"trk"模式定义:

  1. trk: Schema.Types.Mixed
  2. trk: [Schema.Types.Mixed]
  3. trk:[ { type:[Number], index: "2d" }]

希望你能帮助我;-)

6个回答

319
你可以通过以下方式声明 trk: - 要么

trk : [{
    lat : String,
    lng : String
     }]

或者

trk: {类型:Array,“default”:[]}

在第二种情况下,在插入期间制作对象并将其推入数组中,如下所示

db.update({'Searching criteria goes here'},
{
 $push : {
    trk :  {
             "lat": 50.3293714,
             "lng": 6.9389939
           } //inserted data is the object to be inserted 
  }
});

或者您可以通过以下方式设置对象数组:

db.update ({'seraching criteria goes here ' },
{
 $set : {
          trk : [ {
                     "lat": 50.3293714,
                     "lng": 6.9389939
                  },
                  {
                     "lat": 50.3293284,
                     "lng": 6.9389634
                  }
               ]//'inserted Array containing the list of object'
      }
});

5
trk: { type: Array, "default": [] } 对我来说最好!它简单而优雅! - spiralmoon
1
@DpGeek 如果您以这种格式声明数组,则无法直接更新数组字段。为了直接更新数组,我使用了{lat:String,lng:String}子模式。如果您不想要该功能,则trk:{type:Array,“default”:[]}将是最好的选择,否则您必须声明子模式。 - Kundu
1
"默认值为['item1','item2']trk:{type:Array,默认}对我有用。" - Shardul
1
如果'lat'和'lng'字段被定义为数字而不是字符串,它仍然能正常工作吗? - jimijazz
@Apoorvasahay 的搜索条件是一个对象。你可以根据自己的需求来查找项目。一旦你提供了搜索条件,MongoDB 将根据这些条件为你查找项目。 - Kundu
显示剩余2条评论

67

我在使用mongoose时遇到了类似的问题:

fields: 
    [ '[object Object]',
     '[object Object]',
     '[object Object]',
     '[object Object]' ] }

实际上,我在我的模式中使用了"type"作为属性名称:
fields: [
    {
      name: String,
      type: {
        type: String
      },
      registrationEnabled: Boolean,
      checkinEnabled: Boolean
    }
  ]

为了避免这种行为,您需要将参数更改为:
fields: [
    {
      name: String,
      type: {
        type: { type: String }
      },
      registrationEnabled: Boolean,
      checkinEnabled: Boolean
    }
  ]

4
哦耶,我甚至没有想到那个。这解决了我的问题,就在我快要开始在桌子上砸东西之前哈哈,再次感谢。从现在开始,我将避免在我的mongoose模式中使用'type'。 - blackops
你能给一个你试图插入的JSON的例子吗? - owensmartin
2
或者您可以在模式构建器中传递typeKey选项来覆盖类型声明。 - jimijazz

11
为了在模式中创建一个数组,我们需要再创建一个名为monetizationSchema的模式,用于存储一条数据,另一个模式为blogSchema,其中包含一个monetization字段,该字段包含方括号中的monetizationSchema作为数组。
用于存储一条数据的模式。
const monetizationSchema = new Schema({
      amazonUrl: {
        type: String,
        required: true,
      } 
    });

使用 monetization 数组的模式。

const blogSchema = {
  monetization: [
   monetizationSchema
  ],
  image: {
   type: String,
   required: true
  },
  // ... etc
});

9
您可以按照以下方式声明一个数组。
trk : [{
    lat : String,
    lng : String
}]

但它会将 [](空数组)设置为默认值。

如果您不想使用此默认值,则需要将默认值设置为 undefined,如下所示:

trk: {
    type: [{
        lat : String,
        lng : String
    }],
    default: undefined
}

6

我需要解决的问题是存储包含几个字段(地址、书籍、天数、借阅者地址、blk_data)的合同,blk_data 是一个交易列表(块号和交易地址)。以下问题和答案对我有所帮助。我想分享我的代码如下。希望这有所帮助。

  1. 模式定义。请参见 blk_data。
var ContractSchema = new Schema(
    {
        address: {type: String, required: true, max: 100},  //contract address
        // book_id: {type: String, required: true, max: 100},  //book id in the book collection
        book: { type: Schema.ObjectId, ref: 'clc_books', required: true }, // Reference to the associated book.
        num_of_days: {type: Number, required: true, min: 1},
        borrower_addr: {type: String, required: true, max: 100},
        // status: {type: String, enum: ['available', 'Created', 'Locked', 'Inactive'], default:'Created'},

        blk_data: [{
            tx_addr: {type: String, max: 100}, // to do: change to a list
            block_number: {type: String, max: 100}, // to do: change to a list
        }]
    }
);

在MongoDB中为集合创建记录,请参见blk_data。
// Post submit a smart contract proposal to borrowing a specific book.
exports.ctr_contract_propose_post = [

    // Validate fields
    body('book_id', 'book_id must not be empty.').isLength({ min: 1 }).trim(),
    body('req_addr', 'req_addr must not be empty.').isLength({ min: 1 }).trim(),
    body('new_contract_addr', 'contract_addr must not be empty.').isLength({ min: 1 }).trim(),
    body('tx_addr', 'tx_addr must not be empty.').isLength({ min: 1 }).trim(),
    body('block_number', 'block_number must not be empty.').isLength({ min: 1 }).trim(),
    body('num_of_days', 'num_of_days must not be empty.').isLength({ min: 1 }).trim(),

    // Sanitize fields.
    sanitizeBody('*').escape(),
    // Process request after validation and sanitization.
    (req, res, next) => {

        // Extract the validation errors from a request.
        const errors = validationResult(req);
        if (!errors.isEmpty()) {
            // There are errors. Render form again with sanitized values/error messages.
            res.status(400).send({ errors: errors.array() });
            return;
        }

        // Create a Book object with escaped/trimmed data and old id.
        var book_fields =
            {
                _id: req.body.book_id, // This is required, or a new ID will be assigned!
                cur_contract: req.body.new_contract_addr,
                status: 'await_approval'
            };

        async.parallel({
            //call the function get book model
            books: function(callback) {
                Book.findByIdAndUpdate(req.body.book_id, book_fields, {}).exec(callback);
            },
        }, function(error, results) {
            if (error) {
                res.status(400).send({ errors: errors.array() });
                return;
            }

            if (results.books.isNew) {
                // res.render('pg_error', {
                //     title: 'Proposing a smart contract to borrow the book',
                //     c: errors.array()
                // });
                res.status(400).send({ errors: errors.array() });
                return;
            }

            var contract = new Contract(
                {
                    address: req.body.new_contract_addr,
                    book: req.body.book_id,
                    num_of_days: req.body.num_of_days,
                    borrower_addr: req.body.req_addr

                });

            var blk_data = {
                    tx_addr: req.body.tx_addr,
                    block_number: req.body.block_number
                };
            contract.blk_data.push(blk_data);

            // Data from form is valid. Save book.
            contract.save(function (err) {
                if (err) { return next(err); }
                // Successful - redirect to new book record.
                resObj = {
                    "res": contract.url
                };
                res.status(200).send(JSON.stringify(resObj));
                // res.redirect();
            });

        });

    },
];

更新一条记录。请参阅blk_data。
// Post lender accept borrow proposal.
exports.ctr_contract_propose_accept_post = [

    // Validate fields
    body('book_id', 'book_id must not be empty.').isLength({ min: 1 }).trim(),
    body('contract_id', 'book_id must not be empty.').isLength({ min: 1 }).trim(),
    body('tx_addr', 'tx_addr must not be empty.').isLength({ min: 1 }).trim(),
    body('block_number', 'block_number must not be empty.').isLength({ min: 1 }).trim(),

    // Sanitize fields.
    sanitizeBody('*').escape(),
    // Process request after validation and sanitization.
    (req, res, next) => {

        // Extract the validation errors from a request.
        const errors = validationResult(req);
        if (!errors.isEmpty()) {
            // There are errors. Render form again with sanitized values/error messages.
            res.status(400).send({ errors: errors.array() });
            return;
        }

        // Create a Book object with escaped/trimmed data
        var book_fields =
            {
                _id: req.body.book_id, // This is required, or a new ID will be assigned!
                status: 'on_loan'
            };

        // Create a contract object with escaped/trimmed data
        var contract_fields = {
            $push: {
                blk_data: {
                    tx_addr: req.body.tx_addr,
                    block_number: req.body.block_number
                }
            }
        };

        async.parallel({
            //call the function get book model
            book: function(callback) {
                Book.findByIdAndUpdate(req.body.book_id, book_fields, {}).exec(callback);
            },
            contract: function(callback) {
                Contract.findByIdAndUpdate(req.body.contract_id, contract_fields, {}).exec(callback);
            },
        }, function(error, results) {
            if (error) {
                res.status(400).send({ errors: errors.array() });
                return;
            }

            if ((results.book.isNew) || (results.contract.isNew)) {
                res.status(400).send({ errors: errors.array() });
                return;
            }

            var resObj = {
                "res": results.contract.url
            };
            res.status(200).send(JSON.stringify(resObj));
        });
    },
];

2
感谢回复。
我尝试了第一种方法,但是没有任何改变。然后,我尝试记录结果。我一级一级地深入,直到最终找到数据显示的位置。
过了一会儿,我找到了问题所在:当我发送响应时,我通过 .toString() 将其转换为字符串。
我修复了这个问题,现在它工作得非常好。对于误报表示抱歉。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接