在MongoDB中添加多个子文档

编程入门 行业动态 更新时间:2024-10-23 16:29:02
本文介绍了在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中添加多个子文档

本文发布于:2023-11-22 18:33:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文档   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!