如何查询和删除MongoDB中数组字段中的嵌入对象?(How to query for and delete embedded objects in an array field in MongoDB?)
我正在尝试运行一个更新命令,在所有文档的数组字段中找到一个对象,然后从该数组字段中为所有文档提取它。 我尝试了以下命令,它们不返回任何文档(nMatched:0)。
db.users.update({"likedQuizzes.quiz": { $elemMatch: quizObj}},{$pull: {"likedQuizzes.quiz": quizObj }}); db.users.update({"likedQuizzes": { $elemMatch: quizObj}},{$pull: {"likedQuizzes": quizObj }});
在上面的例子中,
quizObj = {"title" : "2 Purple", "author" : "purple-tester1"}
以下是“用户”集合中的示例文档:
{ "_id" : ObjectId("581114330de9ac0c1445cdd6"), "user" : "test-username1", "likedQuizzes" : [ { "quiz" : { "title" : "2 Purple", "author" : "purple-tester1" }, "date" : ISODate("2016-10-26T20:39:02.695Z") }, { "quiz" : { "title" : "1 Purple", "author" : "purple-tester1" }, "date" : ISODate("2016-10-26T20:39:12.374Z") }, { "quiz" : { "title" : "4 Green", "author" : "green-tester1" }, "date" : ISODate("2016-10-26T20:39:25.304Z") }, { "quiz" : { "title" : "3 Green", "author" : "green-tester1" }, "date" : ISODate("2016-10-26T20:39:37.326Z") }, { "quiz" : { "title" : "2 Green", "author" : "green-tester1" }, "date" : ISODate("2016-10-26T20:40:12.964Z") } ] }
换句话说,我只是试图找出一个测验是否在用户的“likesQuizzes”数组中,如果是,则从该数组中删除它(以及它所喜欢的“日期”)。
经过一些研究后,看起来这可能是不可能的,但我还是想问,因为我认为我的模式可能与我研究中提出的例子不同。 提前感谢任何指针。
I am trying to run an update command that finds an object in an array field for all documents and then pulls it from that array field for all documents. I have tried the below commands, which do not return any documents (nMatched: 0).
db.users.update({"likedQuizzes.quiz": { $elemMatch: quizObj}},{$pull: {"likedQuizzes.quiz": quizObj }}); db.users.update({"likedQuizzes": { $elemMatch: quizObj}},{$pull: {"likedQuizzes": quizObj }});
In the above cases
quizObj = {"title" : "2 Purple", "author" : "purple-tester1"}
Here is a sample document from the 'users' collection:
{ "_id" : ObjectId("581114330de9ac0c1445cdd6"), "user" : "test-username1", "likedQuizzes" : [ { "quiz" : { "title" : "2 Purple", "author" : "purple-tester1" }, "date" : ISODate("2016-10-26T20:39:02.695Z") }, { "quiz" : { "title" : "1 Purple", "author" : "purple-tester1" }, "date" : ISODate("2016-10-26T20:39:12.374Z") }, { "quiz" : { "title" : "4 Green", "author" : "green-tester1" }, "date" : ISODate("2016-10-26T20:39:25.304Z") }, { "quiz" : { "title" : "3 Green", "author" : "green-tester1" }, "date" : ISODate("2016-10-26T20:39:37.326Z") }, { "quiz" : { "title" : "2 Green", "author" : "green-tester1" }, "date" : ISODate("2016-10-26T20:40:12.964Z") } ] }
Said another way, I am simply trying to find if a quiz is in a users' "likedQuizzes" array, and if it is, delete it from that array (along with the "date" that it was liked).
After some researching it looks like this might not be possible, but I wanted to ask anyway because I think my schema might be different enough from the examples that came up in my research. Thanks in advance for any pointers.
原文:https://stackoverflow.com/questions/40597119
最满意答案
- 使标记全局化。
- 在创建新的之前检查它是否存在。
- 如果存在则使用.setPosition将其移动到新位置
- 如果它不存在,请在所需位置创建标记。
工作代码片段:
var map = null; var marker = null; var geocoder = new google.maps.Geocoder(); var infowindow = new google.maps.InfoWindow({pixelOffset: new google.maps.Size(0,-34)}); function initialize() { map = new google.maps.Map(document.getElementById('map-canvas'), { center: new google.maps.LatLng(42, -85), zoom: 4 }); } function codeAddress() { var address = document.getElementById('address').value; geocoder.geocode({ 'address': address }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); content = results[0].formatted_address; infowindow.setContent(content); if (marker && marker.setPosition) { marker.setPosition(results[0].geometry.location); } else { marker = new google.maps.Marker({ map: map, icon: { url: 'http://maps.google.com/mapfiles/arrow.png', anchor: new google.maps.Point(10, 34) }, position: results[0].geometry.location }); google.maps.event.addListener(marker, 'click', function(evt) { infowindow.open(map); }); } infowindow.setPosition(results[0].geometry.location); infowindow.open(map); } else { alert('Your search was not successful for the following reason: ' + status); } }); } google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas { height: 500px; width: 500px; margin: 0px; padding: 0px }
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script> <input id="address" value="New York, NY" /> <input id="geocode" type="button" value="Find" onclick="codeAddress()" /> <div id="map-canvas"></div>
- Make the marker global.
- Check if it exists before creating a new one.
- if it exists use .setPosition to move it to the new location
- if it doesn't exist, create a marker at the desired location.
Example that does something close to what you want
working code snippet:
var map = null; var marker = null; var geocoder = new google.maps.Geocoder(); var infowindow = new google.maps.InfoWindow({pixelOffset: new google.maps.Size(0,-34)}); function initialize() { map = new google.maps.Map(document.getElementById('map-canvas'), { center: new google.maps.LatLng(42, -85), zoom: 4 }); } function codeAddress() { var address = document.getElementById('address').value; geocoder.geocode({ 'address': address }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); content = results[0].formatted_address; infowindow.setContent(content); if (marker && marker.setPosition) { marker.setPosition(results[0].geometry.location); } else { marker = new google.maps.Marker({ map: map, icon: { url: 'http://maps.google.com/mapfiles/arrow.png', anchor: new google.maps.Point(10, 34) }, position: results[0].geometry.location }); google.maps.event.addListener(marker, 'click', function(evt) { infowindow.open(map); }); } infowindow.setPosition(results[0].geometry.location); infowindow.open(map); } else { alert('Your search was not successful for the following reason: ' + status); } }); } google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas { height: 500px; width: 500px; margin: 0px; padding: 0px }
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script> <input id="address" value="New York, NY" /> <input id="geocode" type="button" value="Find" onclick="codeAddress()" /> <div id="map-canvas"></div>
相关问答
更多-
Google Maps API v3向每个标记添加一个InfoWindow(Google Maps API v3 adding an InfoWindow to each marker)[2022-08-10]
你在for in循环中有一个很常见的闭包问题 : 封闭在闭包中的变量共享相同的单一环境,所以在调用addListener的click回调时,循环将运行它的过程,并且info变量将被指向最后一个对象,这恰好是最后一个对象InfoWindow创建。 在这种情况下,解决这个问题的一个简单方法是使用InfoWindow来增加你的Marker对象: var marker = new google.maps.Marker({map: map, position: point, clickable: true}); m ... -
在设置标记的行之前移动设置地图的线,因为标记在其调用中使用地图: function initialize() { var location = new google.maps.LatLng(59.272, 10.418); var mapOptions = { center: location, zoom: 8 }; var map = new google.maps.Map(document.getElementById("map-canvas"), mapOption ...
-
var map; var geocoder; var address; function initialize() { map = new GMap2(document.getElementById("map_canvas")); map.setCenter(new GLatLng(40.730885,-73.997383), 15); map.addControl(new GLargeMapControl); GEvent.addListener(map, "click", getAddress); ...
-
navigator.geolocation.getCurrentPosition()是异步的。 像这样重新组织你的代码: var mapOptions = { zoom: 8, mapTypeId: google.maps.MapTypeId.HYBRID } function initialize() { // Check if user support geo-location if (navigator.geolocation) { navigator ...
-
使标记全局化。 在创建新的之前检查它是否存在。 如果存在则使用.setPosition将其移动到新位置 如果它不存在,请在所需位置创建标记。 做一些接近你想要的事情的例子 工作代码片段: var map = null; var marker = null; var geocoder = new google.maps.Geocoder(); var infowindow = new google.maps.InfoWindow({pixelOffset: new google.maps.Size(0, ...
-
你必须精确地想要用于标记的地图,如下所示: var marker = new google.maps.Marker({ map: map, position: myLatlng, title:"Hello World!" }); 祝你今天愉快 ! You have to precise the map you want to use for the marker like this : var marker = new google.maps.Marker({ map: m ...
-
没有地理编码的Google Maps Marker Clusterer Plus(Google Maps Marker Clusterer Plus Without Geocoding)[2023-12-09]
您可以直接使用坐标初始化标记,而无需调用Geocoding API。 // Change address Array to an Array of JS Objects var address = [ { latitude: lat1, longitude: lng1, }, { latitude: lat2, longitude: lng2, }, ... ]; // In function:crea ... -
正如'Geocodezip'所述,谷歌不会共享技术来在其标记上添加标签。 你必须添加自己的。 markerwithlabel这里的'basic.html'例子解决了我的问题。 它允许您添加我编辑代码的标记,以便能够添加多个标记/标记对 var image = { url: './customMarker.jpg', // This marker is 20 pixels wide by 32 pixels high. size: new google.maps.Size(100, ...
-
您在加载函数中声明var map,然后在addMarker函数中不可见 尝试在全局范围内声明var map var map ; var customIcons = { skg: { icon: 'http://mywebsite.nl/img/logo_transparant24.png', shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' } }; functi ...
-
首先,Google Maps API 3不需要API密钥(除非我们引用最近推出的密钥,用于每日地图视图超过25,000的网站,可能需要支付使用费)。 听起来你正在将API 2所需的密钥与API 3的代码混合在一起。 其次,这都是错的。 'i'是postcodes数组的变量,它与结果数组没有关系 for(var i = 0; i < postcodes.length; i++) { var address = postcodes[i].value; geocoder.geocode({'add ...