小编Aza*_*aza的帖子

Mongoose在其引用模型的Model by字段上嵌套查询

在stackoverflow上似乎有很多关于这个主题的Q/A,但我似乎无法在任何地方找到确切的答案.

是)我有的:

我有公司和人员模型:

var mongoose = require('mongoose');
var PersonSchema = new mongoose.Schema{
                        name: String, 
                        lastname: String};

// company has a reference to Person
var CompanySchema = new mongoose.Schema{
                        name: String, 
                        founder: {type:Schema.ObjectId, ref:Person}};
Run Code Online (Sandbox Code Playgroud)

我需要的:

找到所有姓氏为"Robertson"的人成立的公司

我尝试了什么:

Company.find({'founder.id': 'Robertson'}, function(err, companies){
    console.log(companies); // getting an empty array
});
Run Code Online (Sandbox Code Playgroud)

然后我认为Person不是嵌入式的,而是引用的,所以我使用populate来填充创始人Person,然后尝试使用find和'Robertson'lastname

// 1. retrieve all companies
// 2. populate their founders
// 3. find 'Robertson' lastname in populated Companies
Company.find({}).populate('founder')
       .find({'founder.lastname': 'Robertson'})
       .exec(function(err, companies) {
        console.log(companies); // getting an empty array again
    }); …
Run Code Online (Sandbox Code Playgroud)

populate mongoose mongodb node.js

20
推荐指数
2
解决办法
2万
查看次数

算法帮助:如何将数组划分为具有最小可能最大段的N个段(平衡分段)

我在俄罗斯的一个编程论坛上遇到过这个问题,但还没有提出一个优雅的解决方案.

问题:

你有一个N正整数的数组,你需要将它分成M个连续的段,这样最大的段的总和是最小的可能值.按段的总数,我的意思是所有整数的总和.换句话说,我想要一个平衡良好的数组分割,你不希望单个分段过大.

例:

  • 数组:[4,7,12,5,3,16]

  • M = 3,意味着我需要将我的数组分成3个子阵列.

  • 解决方案是:[4,7] [12,5] [3,16],以便最大的段是[3,16] = 19,并且没有其他的分段变体可以产生总数较小的最大段.

另一个例子:

  • 数组[3,13,5,7,18,8,20,1]
  • M = 4

解决方案:[3,13,5] [7,18] [8] [20,1],"最胖"段是[7,18] = 25(纠正我,如果我错了,我编的是这个例子)

我有一种感觉,这是一些经典的CS /数学问题,可能有一些着名人物的名字,如Dijkstra的问题. - 有没有任何已知的解决方案?- 如果没有,你能否提出除暴力强迫之外的其他解决方案,据我所知,时间复杂度,指数.(N ^ M,更具体).

提前谢谢,stackoverflowers.

arrays algorithm optimization

8
推荐指数
2
解决办法
2780
查看次数

即使图像大小没有改变,setImage方法也会改变UIBarButtonItem的宽度

我想在导航栏中添加一个"书签"按钮作为rightBarButtonItem

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Add bookmark button
    UIBarButtonItem *bookmarkBarButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(bookmark:)];
    [bookmarkBarButton setImage:[UIImage imageNamed:@"greyBookmark.png"]];
    [self.navigationItem setRightBarButtonItem:bookmarkBarButton];
    bookmarkBarButton.tintColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0.9f];
    b_bookmarked = false;
}

// Will call this method when the bookmark button is pressed
- (IBAction)bookmark:(id)sender
{
    // Toggle color of bookmark icon on button
    if ( (b_bookmarked = !b_bookmarked) )
    {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"blueBookmark.png"]];
    }
    else
    {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"greyBookmark.png"]];
    }

    // Save bookmark

}
Run Code Online (Sandbox Code Playgroud)

greyBookmark.png和blueBookmark.png都有10x26大小.视图出现时,按钮看起来变窄.但是当我单击此按钮时,按钮会变宽,但图像仍然会发生变化.在bookmark:方法中调用setImage方法时,宽度会更改(恢复为默认大小).我试图通过调用[navigationItem.rightBarButtonItem setWidth:]显式设置宽度 …

iphone objective-c uibarbuttonitem ios

7
推荐指数
1
解决办法
3460
查看次数

同时隐藏状态栏和导航栏,就像在图片应用程序中一样

我有一个包含大量文本的视图,所以我希望允许用户在单击时隐藏statusBar + navigationBar.我真的很喜欢图片应用程序中的隐藏风格,其中statusBar和navigationBar隐藏在一起(不滑动,只是淡出),有一些animationDuration,所以我尝试做类似的事情.这是我在touchesDidBegan方法中所做的:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [UIView setAnimationDuration:0.5];
[UIView beginAnimations:@"" context:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:UIStatusBarAnimationNone];
    [self.navigationController setNavigationBarHidden:(!self.navigationController.navigationBarHidden) animated:NO];
    [UIView commitAnimations];
    self.navigationController.navigationBar.translucent = !self.navigationController.navigationBar.translucent; // this is needed to make bars appear on top of my view.
}
Run Code Online (Sandbox Code Playgroud)

但这并不能同时隐藏条形图.它让它们向下滑动.它与上述方法的版本具有相同的效果:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    // deleted UIView animation, changed animation type to "slide"
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:UIStatusBarAnimationSlide];
    // enabled animation for navBar
    [self.navigationController setNavigationBarHidden:(!self.navigationController.navigationBarHidden) animated:YES];
    self.navigationController.navigationBar.translucent = …
Run Code Online (Sandbox Code Playgroud)

iphone animation statusbar uinavigationbar ios

6
推荐指数
1
解决办法
3331
查看次数