How to custom fields by query in command geoNear? - mongodb

I have a geoNear command:
{
"geoNear": "user",
"near": [
104.4859511,
10.3804459
],
"spherical": true,
"distanceMultiplier": 6371000,
"minDistance": 0.038378767752844,
"maxDistance": 0.47088369172814,
"num": 6
}
I retrieve mongo DB, return some data:
"results": [
{
"dis": 244511.13644192,
"obj": {
"_id": ObjectId("540af074e4b06192b9481436"),
"user_id": NumberInt(219372),
"full_name": "Guest_219372",
"avatar": "",
"birthday": ISODate("2002-01-05T17:00:00.0Z"),
"sex": NumberInt(0),
"feeling": "",
"voice_intro": "",
"location": [
106.684767,
10.785003
],
"last_update": NumberInt(0),
"last_update_location": NumberInt(1410002680)
}
}
]
I want to limit the data by only a few fields, using "query" with help in mongo official site but it does not return data. I can not check the right and wrong of the query in command geoNear. I have tried to aggregation and found that it supports $ project, geoNear things simpler. I'm using minDistance want your client, do not support aggregation minDistance, forcing me to take command geoNear.
{
"geoNear": "user",
"near": [
104.4859511,
10.3804459
],
"spherical": true,
"distanceMultiplier": 6371000,
"minDistance": 0.038378767752844,
"maxDistance": 0.47088369172814,
"num": 6,
"query": {
"find": {
"user_id": true,
"_id": false
...
}
}
}

Related

How to use Multiple $geoNear in mongodb?

How to use $geoNear in mongodb?
I want to use geoNear 3 times and i want to get all result.
I try to made an aggregationlike this.
but it occurs an error "$geoNear requires a 'near' option as an Array"
Thank you for help
db.getCollection('collection').aggregate([
{
"$geoNear": {
"$or": [
{
"near": {
"type": "Point",
"coordinates": [
126.7384307,
35.9672442
]
},
"maxDistance": 1000,
"query": {
"distance": 1000,
"use_noti": true,
},
"distanceField": "location",
},
{
"near": {
"type": "Point",
"coordinates": [
126.7384307,
35.9672442
]
},
"maxDistance": 5000,
"query": {
"distance": 5000,
"use_noti": true,
},
"distanceField": "location",
},
]
}
},
])
```

How to get result using stable sort using mongodb

I am working on making a query which can sort the result after grouping keys in MongoDB.
Following is the example data in DB
[
{
"_id": ObjectId("5a934e000102030405000000"),
"code": "code",
"groupId": "L0LV7ENT",
"version": {
"id": "1.0.0.0"
},
"status": "Done",
"type": "main"
},
{
"_id": ObjectId("5a934e000102030405000001"),
"code": "code",
"groupId": "L0LV7ENT",
"version": {
"id": "2.0.0.0"
},
"status": "Done",
"type": "main"
},
{
"_id": ObjectId("5a934e000102030405000002"),
"code": "code",
"groupId": "F6WJ9QP7",
"version": {
"id": "1.1.0.0"
},
"status": "Done",
"type": "main"
}
]
Here, I would like to sort the result in ascending order according to the version.id and to group the result according to the groupId.
Hence, I used the following query
db.collection.aggregate([
{
"$match": {
"$and": [
{
"type": "main",
"code": {
"$in": [
"code"
]
},
"status": {
"$in": [
"Done",
"Completed"
]
},
"groupId": {
"$in": [
"L0LV7ENT",
"F6WJ9QP7"
]
}
}
]
}
},
{
"$sort": {
"_id": 1,
"version.id": 1
}
},
{
"$group": {
"_id": {
"groupId": "$groupId"
},
"services": {
"$push": "$$ROOT"
}
}
}
])
But the result I am getting is not stable. Sometimes I see, the data with "_id": ObjectId("5a934e000102030405000002") coming first then ObjectId("5a934e000102030405000000") and ObjectId("5a934e000102030405000001").
It seems intermmitent. Is there any way to get a stable result?
EDIT
You can try it here
From the documentation:
$group does not order its output documents.
So you will need to sort after the group stage to have a deterministic output order.

Using GraphQL,Springboot,MongoDB.The json is 1000+ lines deeply nested.Instead of updating whole doc,need to update specific key-value at any position

Requirement for mutation is to behave like upsert. For example in below json mutation is required to change "status" under Rooms->Availability section. Which I do not want to hard code like
db.collection.update({
'Rooms.Availability.status':true
},{
$set : {
'Rooms.Availability.status':False
}
})
for specific Array Index because there are possibilities of not having "status" or "Availability" key in some other document.
Below is similar JSON structure. Keys can be different in other JSON documents within same collection.
#GraphQLMutation(name = "updateHotelDetails")
public Hotel updateHotelDetails(Hotel h){ // Do I need to pass whole object as argument or only specific key-value?
mongoTemplate.updateFirst(....); // How can I write update code without hard coding?
}
Document 1:
{
"_id" : ObjectId("71testsrtdtsea6995432"),
"HotelName": "Test71testsrtdtsea699fff",
"Description": ".....",
"Address": {
"Street": "....",
"City": "....",
"State": "...."
},
"Rooms": [
{
"Description": "......",
"Type": ".....",
"Price": "....."
"Availability": [
"status": false,
"readOnly": false
]
},
{
"Description": "......",
"Type": "....",
"Price": "..."
"Availability": [
"status": true,
"readOnly": false
]
"newDynamickey": [
{}
]
},
]
"AdditionalData": [
{
"key1": "Vlaue1",
"key2":"Value2"
},
{...}
]
}

How to update sub document which is nested in array of array in MongoDB

I have a requirement to update sub document of my mongoDB document which is nested as array of array as shown below in json structure:
{
"_id": "53cf4e3dae92ac6561807f6d",
"results": {
"trips": [
{
"_id": 1,
"name": "A",
"address": [
{
"_id": 123,
"value": {
"allowed": true,
"optional": false
}
},
{
"_id": 456,
"value": {
"allowed": false,
"optional": false
}
}
]
},
{
"_id": 2,
"name": "B",
"address": [
{
"_id": 789,
"value": {
"allowed": true,
"optional": false
}
},
{
"_id": 567,
"value": {
"allowed": false,
"optional": false
}
}
]
}
]
}
}
Now, I want to update results.trips.address._id =456 with new json element "value" object. I can think of using update query with $elemMatch but may be I am not using correctly so, it is not working for me.
Also, I would be doing all this in java using spring data 1.4.x and mongoDB version is 2.6.
Any help would be highly appreciated.
Query which I am using is:
db.collection.update({"_id" : ObjectId("53cf4e3dae92ac6561807f6d"), "results.trips.address":{$elemMatch : {"results.trips.address._id":456}}},{$set : {"results.trips.address.value":{"allowed": true, "optional": true}}});

Elasticsearch nested query

I'm new to elasticsearch, managed to set it up and import recordset from my mongodb collection using the river plugin. For a start, I want to query against the "desc" field but just can't manage to get the query .. not sure if the problem is driven by the way index was defined.. can anyone help please?
Sample recordset in elastic search looks like this
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 107209,
"max_score": 1,
"hits": [
{
"_index": "shiv",
"_type": "shiv",
"_id": "iG1eIzN7RGO7hFfxTlnLuA",
"_score": 1,
"_source": {
"_id": {
"$oid": "50901d7f485bf7bd1c000021"
},
"brand": "",
"category": {
"$ref": "categories",
"$id": {
"$oid": "4fbd2221758cb11d14000174"
}
},
"comments": [],
"count_comment": 0,
"count_fav": 2,
"count_hotness": 1.46,
"count_rekick": 0,
"count_share": 0,
"country": {
"$ref": "countries",
"$id": {
"$oid": "4fec98f7758cb18c6e0002c9"
}
},
"currency": "pound",
"desc": "A men's automatic watch, this Seamaster Bond model features a Co-Axial escapement and date function. Its blue dial is teamed with a stainless steel case and bracelet for a look that's sporty and refined.",
"gender": "male",
"ident": "omega-seamaster-diver-bond-men-s-automatic-watch---ernest-jones-1351622015",
"img_url": "http://s7ondemand4.scene7.com/is/image/Signet/5735793?$detail$",
"lifestyles": [
{
"$ref": "lifestyles",
"$id": {
"$oid": "508ff6ca485bf73112000060"
}
}
],
"location": "United Kingdom",
"owner": {
"$ref": "accounts",
"$id": {
"$oid": "50742fd8485bf74b7a00213f"
}
},
"price": 2400,
"store": "ernestjones.co.uk",
"tags": [
"ernest-jones",
"bond"
],
"timestamp_creation": 1351622015,
"timestamp_exp": 1356825600,
"timestamp_update": 1351622015,
"title": "Omega Seamaster Diver Bond men's automatic watch - Ernest Jones",
"url": "http%3A%2F%2Fwww.ernestjones.co.uk%2Fwebstore%2Fd%2F5735793%2Fomega%20seamaster%20diver%20bond%20men%27s%20automatic%20watch%2F%3Futm_source%3Dgooglebase%26utm_medium%3Dfeedmanager%26cm_mmc%3DFroogle-_-CKB-_-nurses_fobs-_-watches%26cm_mmca1%3Domega%26cm_mmca2%3Dmale%26cm_mmca3%3Dadult"
}
}
]
}
}
The mapping of the index "shiv" looks like
{
"shiv": {
"properties": {
"$oid": {
"type": "string"
}
}
}
}
Thanks again
There are lots of ways to query, have you tried a match query?
Using curl or a rest client of your choice...
http://[host]:9200/[index_name]/[doc_type]/_search
{
"query" : {
"match" : {
"desc" : "some value you want to find in desc"
}
}
}