通过ObjectId查询猫鼬模式

通过ObjectId查询猫鼬模式

本文介绍了通过ObjectId查询猫鼬模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次需要您的帮助,希望对于这个项目,我在这里拥有的答案将是最后的答案.我已经看到这是一个相当普遍的问题,但是我尝试了另一个,并通过 Google网上论坛,但解决方案对我而言无效.

Going to need your help again, hopefully for this project, the answer to what I'm having here will be the last. I've seen it's a fairly commonly asked question, but I've tried the tips on another Stack Overflow post, and one by the Google Group, but the solutions haven't worked for me.

我的代码有点像:

mongoose = require('mongoose');
Schema = mongoose.Schema;
mongoose.connect(MONGO_SERVER);
ObjectId = Schema.ObjectId;

var RacesSchema = new Schema({
    venue_id        : { type: mongoose.Schema.ObjectId, ref: 'Venues' },
    racetype            : String
});
var races = mongoose.model('Races', RacesSchema );

function test() {
    var MyObjectId = require('mongoose').Types.ObjectId;
    queryVenue = new MyObjectId("50e3dcdbf30375180c078f64");

    races.find({venue_id: queryVenue, racetype:'Test'})
    .exec(function(err,data) {
}

test();

但是我没有结果,我知道有结果.

But I get no results, which I know there is.

非常感谢!

更新

已经最小化了上面的代码示例,如果我自己查询字符串值,仅查询ObjectId是失败的地方,并且我知道它存在,则此测试有效.

Have minimized the above code sample, this test works if I query the string value on its own, just querying for an ObjectId is where it fails, and I know it exists.

JSON更新

这是我要找的东西:

{
    "_id" : ObjectId("50e3dcddf30375180c078f85"),
    "venue_id" : "50e3dcdbf30375180c078f64",
    "racetype" : "A"
}

突然之间,我相信我的答案已经很清楚了.仅仅是因为placen_id实际上是一个字符串吗?如果是这样,我能否保持我的猫鼬模式不变,并在进行字符串查找时对查询进行强制转换?还是应该更改这些值的存储方式(来自我开发的单独的.net应用程序)以作为ObjectId插入?

And all of a sudden, I believe my answer has become clear to me. Is it simply because the venue_id is actually a string? And if so, can I keep my mongoose schema the way it is, and do casting on the query at the point of doing the find of being a string? Or should I change the way these values are being stored (from a separate .net application I developed) to be inserted as ObjectId's?

目前,对于另一个查询,当前的猫鼬模式和[实际设置]数据库的方式,使用populate()可以很好地用此模型填充被引用表(venue_id)的结果当前设置的,只是上面的查询有所不同,我没有指定venue_id ...

Currently right now for another query, the current mongoose schema and the way the database is [actually set up], using populate() works quite well to fill the results of the referenced table (venue_id) with the way this model is currently set up, only difference being on the above query, I don't specify the venue_id...

谢谢.

推荐答案

对,出现问题是因为架构(ObjectId)中venue_id的数据类型与文档(字符串)中的数据类型不匹配.因此,find正在查找ObjectId值,但未找到匹配项,因为它是文档中的字符串.

Right, the problem is happening because the data type of venue_id in the schema (ObjectId) doesn't match the one in the doc (String). So the find is looking for ObjectId values but doesn't find a match because it's a string in the doc.

正确的解决方法是编写一个小程序以将文档中的venue_id值更新为ObjectIds而不是字符串,然后您的查询将起作用.这还将使这些字段的大小从24个字节缩小到12个.

The right fix for this is to write a little program to update the venue_id values in your docs to be ObjectIds instead of strings and then your query will work. That will also shrink the size of those fields from 24 bytes to 12.

这篇关于通过ObjectId查询猫鼬模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:47