mongodb-query

Is there a way to display timestamp in unix format to ISODate?

♀尐吖头ヾ 提交于 2019-12-04 17:11:21
问题 We stored a date using unix timestamp in MongoDB, how do I get the date when I do the query? Is there a way to display timestamp in ISODate format? 回答1: Background A unixtime value represents seconds since the epoch (Jan 1, 1970). A JavaScript Date() represents milliseconds since the epoch. In MongoDB, ISODate() is a convenience wrapper for Date() that allows you to create dates from ISO strings in the mongo shell. If you use new Date() in the shell, it will return an ISODate() . Conversion

Geonear sort by distance and time

假如想象 提交于 2019-12-04 16:47:47
I have the following data: { "_id" : ObjectId("55a8c1ba3996c909184d7a22"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T08:50:02.892Z"), "palce" : "aa", "sex" : 1, "longdis" : 1, "location" : [ 106.607312, 29.575281 ] } { "_id" : ObjectId("55a8c1ba3996c909184d7a24"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T08:50:02.920Z"), "palce" : "bbb", "sex" : 1, "longdis" : 1, "location" : [ 106.589896, 29.545098 ] } { "_id" : ObjectId("55a8c1ba3996c909184d7a25"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" :

Mongo Query On Multiple Fields Of Sub-Document

被刻印的时光 ゝ 提交于 2019-12-04 16:35:44
问题 Suppose I have a collection like this: { "arr" : [ { "name" : "a", "num" : 1 }, { "name" : "a", "num" : 2 } ] }, { "arr" : [ { "name" : "b", "num" : 1 }, { "name" : "a", "num" : 2 } ] }, { "arr" : [ { "name" : "b", "num" : 1 }, { "name" : "b", "num" : 2 } ] } and I want to find all documents who's arr contains a sub-document with a name = "b" and num = 2. If I do a query like this: db.collection.find({ $and: [ { "arr.name": "b" }, { "arr.num": 2 } ] }); it will return all documents in the

mongodb query: $size with $gt returns always 0

空扰寡人 提交于 2019-12-04 16:02:31
I wonder what can I do to enable inequalities on the size of arrays in mongodb queries. For example, I have the following two queries: > db.userStats.count({sessions:{$size:1}}) 1381 > db.userStats.count({sessions:{$size:{$gt:0}}}) 0 If $gt worked as I intend the result would be different for the second query, leading to a number that is at least 1381. What is wrong with the second query and how can I fix it to achieve the result I want? thanks You cannot do that as is actually referenced in the documentation for $size . So that operator alone evaluates a "literal" result and cannot be use in

MongoDB Group and Subtract Values from Different Documents

放肆的年华 提交于 2019-12-04 15:32:19
In my application I have the following documents { "timestamp": ISODate("2015-09-17T21:14:35.0Z"), "sensor": "gas-in", "value": 2.5, }, { "timestamp": ISODate("2015-09-17T21:14:35.0Z"), "sensor": "gas-out", "value": 2.0, }, { "timestamp": ISODate("2015-09-17T21:20:35.0Z"), "sensor": "gas-in", "value": 6.3, }, { "timestamp": ISODate("2015-09-17T21:20:35.0Z"), "sensor": "gas-out", "value": 0.8, } ...etc... How can I return the difference (gas-in) - (gas-out) grouped per timestamp ? I am trying to write a function that returns it like this: { "timestamp": ISODate("2015-09-17T21:14:35.0Z"),

Want to Update Array of Array Object Value in Mongo

别等时光非礼了梦想. 提交于 2019-12-04 15:31:15
I have the following MongoDB document: { "_id": ObjectId(), "sku": "V4696-DR-V33", "options": [ { "sku": "8903689984338", "stores": [ { "code": "AND1", "zipcode": "110070", "inventory": -1000 }, { "code": "AND2", "zipcode": "201010", "inventory": -1000 }, { "code": "AND3", "zipcode": "411001", "inventory": -1000 }, { "code": "AND4", "zipcode": " 700020", "inventory": -1000 }, { "code": "AND5", "zipcode": "110015", "inventory": -1000 } ], "price": 2199, "_id": ObjectId(), "size": "14" }, { "sku": "1742564789", "stores": [ { "code": "AND1", "zipcode": "110070", "inventory": -1000 }, { "code":

Filter Documents by Distance Stored in Document with $near

∥☆過路亽.° 提交于 2019-12-04 15:20:12
I am using the following example to better explain my need. I have a set of points(users) on a map and collection schema is as below { location:{ latlong:[long,lat] }, maxDistance:Number } i have another collection with events happening in the area. schema is given below { eventLocation:{ latlong:[long,lat] } } now users can add their location and the maximum distance they want to travel for to attend an event and save it. whenever a new event is posted , all the users satisfying their preferences will get a notification. Now how do i query that. i tried following query on user schema { $where

How to create a website with a searchbar to query a mongo database?

爱⌒轻易说出口 提交于 2019-12-04 14:58:10
I have a large mongoDB database set up and am trying to create a website where a user can use a searchbar to query the database remotely and have the results posted on the site (strictly read-only). I have experience with databases for data analysis, but have never created a website for querying results. I'm don't have any experience with web development and don't know what platforms (PHP? node.js?) to use. Thanks in advance. There are the following steps to the problem: Create the front-end, which will consist of HTML, CSS, and Javascript. Beginners often find it easiest to work with jQuery

$lookup when foreign field is an array

ⅰ亾dé卋堺 提交于 2019-12-04 14:53:59
I have two collections. Sports : { "_id" : ObjectId("5bcaf82120e047301b443c06"), "item_name" : "Football", "item_icon" : "ps_icon_football.png", "slot_divisions" : { "0" : { "div_id" : ObjectId("5bd037ec5021b307e793f7b3"), "description" : "5x5" }, "1" : { "div_id" : ObjectId("5bd0384b5021b307e793f7b4"), "description" : "7x7" } }) Booking : { "_id" : ObjectId("5be015bd870565038c7660f4"), "spot" : ObjectId("5bd825cb8705651b1c2f17e2"), "date" : ISODate("2018-11-13T10:04:45.000Z"), "slots" : [ { "booking_id" : ObjectId("5be015bd870565038c7660f3"), "slot_id" : ObjectId("5bd0384b5021b307e793f7b4"),

MongoDB Aggregation Project check if array contains

China☆狼群 提交于 2019-12-04 14:33:40
I have following document: { _id : 21353456, username : "xy", text : "asdf", comments : [ { username : "User1", text : "hi", }, { username : "User2", text : "hi1", }, { username : "User3", text : "hi2", }, { username : "User4", text : "hi3", } ] } Now I want to get the username, text and comments with aggregation and project. In addition I also want a boolean if the comments array contains an username with "User1". I have this, but it doesn't work. db.posttest.aggregate( [ { $project: { username: 1, text: 1, comments : 1, hasComment: { $eq: [ "comments.$.username", "User1" ] }, _id: 0 } } ] )