如何在流星的mongo查询中使用new Meteor.Collection.ObjectID()? [英] How do I use new Meteor.Collection.ObjectID() in my mongo queries with meteor?

查看:79
本文介绍了如何在流星的mongo查询中使用new Meteor.Collection.ObjectID()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Collection,其中的文档带有嵌套对象数组. 这是用于填充数据库的装置代码:

I have a Collection that has documents with an array of nested objects. Here is fixture code to populate the database:

if (Parents.find().count() == 0) {
    var parentId = Parents.insert({
        name: "Parent One"
    });
    Children.insert({
        parent: parentId,
        fields: [
            {
                _id: new Meteor.Collection.ObjectID(),
                position: 3,
                name: "three"
            },
            {
                _id: new Meteor.Collection.ObjectID(),
                position: 1,
                name: "one"
            },
            {
                _id: new Meteor.Collection.ObjectID(),
                position: 2,
                name: "two"
            },

        ]
    });
}

您可能会问自己,当我可以根据名称进行更新时,为什么我什至需要一个ObjectID.这是我目前正在处理的一个复杂得多的架构的简化示例,并且将动态创建嵌套对象,因此一定要使用ObjectID来完成这项工作.

You might be asking yourself, why do I even need an ObjectID when I can just update based off of the names. This is a simplified example to a much more complex schema that I'm currently working on and the the nested object are going to be created dynamically, the ObjectID's are definitely going to be necessary to make this work.

基本上,我需要一种方法来保存具有唯一ID的嵌套对象,并能够通过其_id更新字段.

Basically, I need a way to save those nested objects with a unique ID and be able to update the fields by their _id.

这是我的方法,也是我从浏览器控制台进行的呼叫:

Here is my Method, and the call I'm making from the browser console:

Meteor.methods({
  upChild: function( options ) {
        console.log(new Meteor.Collection.ObjectID());
        Children.update({_id: options._id, "fields._id": options.fieldId }, {$set: {"fields.$.position": options.position}}, function(error){
            if(error) {
                console.log(error);
            } else {
                console.log("success");
            }
        });
    }
});

我从控制台拨打的电话:

My call from the console:

Meteor.call('upChild', {
  _id: "5NuiSNQdNcZwau92M",
  fieldId: "9b93aa1ef3868d762b84d2f2",
  position: 1
});

这是html的屏幕截图,我在其中渲染了Parents and Children集合的所有数据:

And here is a screenshot of the html where I'm rendering all of the data for the Parents and Children collections:

推荐答案

仅观察一下,因为类似的原因,我正在寻找如何生成唯一ID的客户端.我发现调用new Meteor.Collection.ObjectID()返回的是'ObjectID("abc ...")'形式的对象.通过将Meteor.Collection.ObjectID()._ str分配给_id,我得到的字符串是'abc ...',这正是我想要的.

Just an observation, as I was looking how generate unique IDs client side for a similar reason. I found calling new Meteor.Collection.ObjectID() was returning a object in the form 'ObjectID("abc...")'. By assigning Meteor.Collection.ObjectID()._str to _id, I got string as 'abc...' instead, which is what I wanted.

我希望这会有所帮助,并且我很想知道是否有人可以更好地处理此问题?

I hope this helps, and I'd be curious to know if anyone has a better way of handling this?

Jason

这篇关于如何在流星的mongo查询中使用new Meteor.Collection.ObjectID()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆