在MongoDB中添加多个子文档 [英] Add multiple sub-documents in MongoDB

查看:164
本文介绍了在MongoDB中添加多个子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个客户数据加载器,客户可以在其中拥有多个地址。如果找不到客户,我会创建它并添加地址。如果客户存在,我只需添加新地址,如下所示:

I am working on a customer data loader, where customers can have multiple addresses. If the customer is not found, I create it and add the address. If the customer exists, I simply add the new address, like this:

    DBObject findCustomer = new BasicDBObject();
    findCustomer.put("email", custEmail);

    //check for existing customer
    DBObject newCustomer = customerCollection.findOne(findCustomer);

    if (newCustomer == null) {
        //INSERT    
        newCustomer = new BasicDBObject();
        newCustomer.put("firstname", firstname);
        newCustomer.put("lastname", lastname);
        newCustomer.put("email", custEmail);
        newCustomer.put("password", custData.getPassword());
        newCustomer.put("softwaretime", new Date());
    }

    DBObject newAddress = new BasicDBObject();
    City tempCity = new City();
    tempCity = addressData.getCity();

    newAddress.put("type", addressData.getType());
    newAddress.put("line1", addressData.getLine1());
    newAddress.put("line2", addressData.getLine2());
    newAddress.put("city", tempCity.getCity());
    newAddress.put("state", tempCity.getState());
    newAddress.put("postal", tempCity.getZip());
    newAddress.put("country", tempCity.getCountry());

    newCustomer.put("address", newAddress);

    customerCollection.save(newCustomer);

这适用于新客户。问题是当客户已经存在时,新地址会覆盖现有地址。

This works for new customers. The problem is that when the customer already exists, the new address overwrites the existing address.

如何将新地址添加到客户,以便它保持多个地址?

How can I add the new address to the customer, so that it will keep multiple addresses?

从我发现的内容来看,我应该能够通过shell推送来实现这一目标。但是我没有在BasicDBObject上看到推作为方法。

From what I've found, I should be able to accomplish this with a "push" via the shell. But I don't see "push" as method on the BasicDBObject.

推荐答案

你的逻辑变得更简单了出。您不需要通过电子邮件获取客户(我假设这是您对客户的唯一识别密钥)只需更新。

Your logic can be much simpler, as it turns out. You don't need to fetch customer by "email" (I'm assuming that this is your unique identifying key for Customer) just update.

findCustomer.put("email", custEmail); // search query for the customer email

// construct your newAddress object the same way you already are

BasicDBObject custMod = new BasicDBObject();
custMod.put("$addToSet", newAddress);
customerCollection.update(findCustomer, custMod, true /* upsert */, false /* multi */ );

现在你的逻辑方式存在的大问题是多线程无效。您可以检查客户,它不会在那里。在构造要插入它的对象时,另一个线程已经在做了。由于地址对象是一个数组而不是一个字段,因此使用$ addToSet将添加到数组(如果存在),但如果它正在创建一个新客户,那么它将创建一个数组地址。

Big problem with the way you have your logic now is it won't work multi-threaded. You can check for customer, and it won't be there. While you are constructing the object to insert it, another thread is already doing it. Since address object is an array rather than a single field, using $addToSet will add to the array if it exists, but if it's creating a new customer, then it will create address as an array.

这篇关于在MongoDB中添加多个子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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