流星在mongodb中返回数组的长度

时间:2022-02-14 17:37:40

In my users profile collection I have array with image objects in it.

在我的用户配置文件集合中,我有包含图像对象的数组。

A user can have a max of 3 images in their profile collection. If the user has 3, throw an error that the maximum has been reached. The user has the option to remove an image themselves in the frontend.

用户的个人资料集合中最多可以有3个图像。如果用户有3,则抛出已达到最大值的错误。用户可以选择在前端删除图像。

I thought the solution would be to check the length of the array with $size. if it's less then 3, insert the image, else throw error.

我认为解决方案是用$ size检查数组的长度。如果它小于3,插入图像,否则抛出错误。

I'm using the tomi:upload-jquery package.

我正在使用tomi:upload-jquery包。

client:

  Template.uploadImage.helpers({
    uploadUserData: function() {
        return Meteor.user();
    },
    finishUpload: function() {
        return {
            finished: function(index, fileInfo, context) {

                Meteor.call('insert.profileImage', fileInfo, function(error, userId) {
                    if (error) {
                        // todo: display modal with error
                        return console.log(error.reason);
                    } else {
                        // console.log('success ' +userId);
                        // console.log('success ' + fileInfo);
                    }
                });
            }
        };
    }
});

The method (server) I use:

我使用的方法(服务器):

'insert.profileImage': function(postImage) {
    check(postImage, Object);

    // check array profile.images max 3

    Meteor.users.update(this.userId, {
        $push: {
            'profile.images': postImage
        }
    });
},

2 个解决方案

#1


You may do it with a function using the $where operator:

您可以使用$ where运算符使用函数执行此操作:

'insert.profileImage': function(postImage) {
    var updateResults;
    check(postImage, Object);

    updateResults = Meteor.users.update(
    {
        _id : this.userId,
        $where : 'this.profile.images.length < 3' //'this' is the tested doc
    },
    {
        $push: {
            'profile.images': postImage
        }
    });

    if(updateResults === 0) {
       throw new Meteor.Error('too-many-profile-images', 
         'A user can only have up to 3 images on his/her profile');
    }
},

The Mongo docs warns about potential performance issues (if you run a JavaScript function on all documents of the store, you're in for bad surprises) but since we also search by _id I guess it should be fine.

Mongo文档警告潜在的性能问题(如果你在商店的所有文档上运行JavaScript函数,你会遇到意外的错误)但是因为我们也在_id搜索,我想它应该没问题。

This way, the update just doesn't run if the user has too many images. You can also check the number of affected document (the return value of the update) to know if something happened. If nothing (returns 0) happened, there's not many possibilities: The user has too many images.

这样,如果用户的图像太多,则更新不会运行。您还可以检查受影响文档的数量(更新的返回值)以了解是否发生了某些事情。如果没有(返回0)发生,则可能性不大:用户图像太多。

#2


Use the $exists operator to check the existence of all documents that have at least a fourth profile image array element (index position 3) with the dot notation. For example you could use it to check whether the size of the profile.image array is greater than 3 with the find() method as follows:

使用$ exists运算符检查是否存在具有至少第四个配置文件图像数组元素(索引位置3)的所有文档,并带有点表示法。例如,您可以使用find来检查profile.image数组的大小是否大于3,如下所示:

var hasSizeGreaterThanThree = Meteor.users.find( 
    {
        '_id': this.userId, 
        'profile.image.3': { '$exists': true }
    }).count() > 0;

So you could use that in your code as:

因此,您可以在代码中使用它:

'insert.profileImage': function(postImage) {
    check(postImage, Object);

    // check array profile.images max 3
    var hasSizeGreaterThanThree = Meteor.users.find( 
        {
            '_id': this.userId, 
            'profile.image.3': { '$exists': true }
        }).count() > 0;

    if (!hasSizeGreaterThanThree){
        Meteor.users.update(this.userId, {
            $push: {
                'profile.images': postImage
            }
        });
    }
},

#1


You may do it with a function using the $where operator:

您可以使用$ where运算符使用函数执行此操作:

'insert.profileImage': function(postImage) {
    var updateResults;
    check(postImage, Object);

    updateResults = Meteor.users.update(
    {
        _id : this.userId,
        $where : 'this.profile.images.length < 3' //'this' is the tested doc
    },
    {
        $push: {
            'profile.images': postImage
        }
    });

    if(updateResults === 0) {
       throw new Meteor.Error('too-many-profile-images', 
         'A user can only have up to 3 images on his/her profile');
    }
},

The Mongo docs warns about potential performance issues (if you run a JavaScript function on all documents of the store, you're in for bad surprises) but since we also search by _id I guess it should be fine.

Mongo文档警告潜在的性能问题(如果你在商店的所有文档上运行JavaScript函数,你会遇到意外的错误)但是因为我们也在_id搜索,我想它应该没问题。

This way, the update just doesn't run if the user has too many images. You can also check the number of affected document (the return value of the update) to know if something happened. If nothing (returns 0) happened, there's not many possibilities: The user has too many images.

这样,如果用户的图像太多,则更新不会运行。您还可以检查受影响文档的数量(更新的返回值)以了解是否发生了某些事情。如果没有(返回0)发生,则可能性不大:用户图像太多。

#2


Use the $exists operator to check the existence of all documents that have at least a fourth profile image array element (index position 3) with the dot notation. For example you could use it to check whether the size of the profile.image array is greater than 3 with the find() method as follows:

使用$ exists运算符检查是否存在具有至少第四个配置文件图像数组元素(索引位置3)的所有文档,并带有点表示法。例如,您可以使用find来检查profile.image数组的大小是否大于3,如下所示:

var hasSizeGreaterThanThree = Meteor.users.find( 
    {
        '_id': this.userId, 
        'profile.image.3': { '$exists': true }
    }).count() > 0;

So you could use that in your code as:

因此,您可以在代码中使用它:

'insert.profileImage': function(postImage) {
    check(postImage, Object);

    // check array profile.images max 3
    var hasSizeGreaterThanThree = Meteor.users.find( 
        {
            '_id': this.userId, 
            'profile.image.3': { '$exists': true }
        }).count() > 0;

    if (!hasSizeGreaterThanThree){
        Meteor.users.update(this.userId, {
            $push: {
                'profile.images': postImage
            }
        });
    }
},