MongoDB find $near in object array
I started to work with MongoDB API, beacause we are using Azure Cosmos DB.
Tried examples using $near in MongoDB with the basic structure {key:"A": localtion:{type:"Point", coordinates:[1,2]}}, and works well. The ploblem is when i need to use an array of locations.
I'm triying to execute this query without result. What i'm doing bad?
db.places.insert( {
id:1,
name: "AAAAAAAAAAA",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7191 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:2,
name: "BBBBBBBBBBB",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7193 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:3,
name: "CCCCCCCCCCCCC",
locals:[
{
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
}
],
category: "Parks"
} );
//Create index
db.places.createIndex({ "locals.location" : "2dsphere" })
//Query without result
db.places.find(
{
"locals.location":
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
The places has many locals, that's why I can have many objects inside.
I hope some one can help me.
Cheers.
mongodb geolocation azure-cosmosdb geojson 2dsphere
add a comment |
I started to work with MongoDB API, beacause we are using Azure Cosmos DB.
Tried examples using $near in MongoDB with the basic structure {key:"A": localtion:{type:"Point", coordinates:[1,2]}}, and works well. The ploblem is when i need to use an array of locations.
I'm triying to execute this query without result. What i'm doing bad?
db.places.insert( {
id:1,
name: "AAAAAAAAAAA",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7191 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:2,
name: "BBBBBBBBBBB",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7193 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:3,
name: "CCCCCCCCCCCCC",
locals:[
{
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
}
],
category: "Parks"
} );
//Create index
db.places.createIndex({ "locals.location" : "2dsphere" })
//Query without result
db.places.find(
{
"locals.location":
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
The places has many locals, that's why I can have many objects inside.
I hope some one can help me.
Cheers.
mongodb geolocation azure-cosmosdb geojson 2dsphere
2
You are using CosmosDB? If so then I don't see this working. There is a supported way for MongoDB with the aggregation framework, but this is not compatible with the "compatibility layer" of CosmosDB. These are two completely different products, despite the marketing hype claiming otherwise.
– Neil Lunn
Nov 13 '18 at 22:03
1
See also How to query $near in CosmosDB via mongoDB protocol. As for "an array of sub-documents with location", I would not recommend it. Your example is trivial ( for MongoDB at least ) but even the unsupported$geoNear
can only identify at most one match from the sub-documents. See $geoNear matching nearest array for that one.
– Neil Lunn
Nov 13 '18 at 22:19
Hi,does my answer helps you?
– Jay Gong
Nov 15 '18 at 1:17
Thx @NeilLunn you are right.
– Paul Escarcena
Nov 15 '18 at 14:01
add a comment |
I started to work with MongoDB API, beacause we are using Azure Cosmos DB.
Tried examples using $near in MongoDB with the basic structure {key:"A": localtion:{type:"Point", coordinates:[1,2]}}, and works well. The ploblem is when i need to use an array of locations.
I'm triying to execute this query without result. What i'm doing bad?
db.places.insert( {
id:1,
name: "AAAAAAAAAAA",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7191 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:2,
name: "BBBBBBBBBBB",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7193 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:3,
name: "CCCCCCCCCCCCC",
locals:[
{
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
}
],
category: "Parks"
} );
//Create index
db.places.createIndex({ "locals.location" : "2dsphere" })
//Query without result
db.places.find(
{
"locals.location":
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
The places has many locals, that's why I can have many objects inside.
I hope some one can help me.
Cheers.
mongodb geolocation azure-cosmosdb geojson 2dsphere
I started to work with MongoDB API, beacause we are using Azure Cosmos DB.
Tried examples using $near in MongoDB with the basic structure {key:"A": localtion:{type:"Point", coordinates:[1,2]}}, and works well. The ploblem is when i need to use an array of locations.
I'm triying to execute this query without result. What i'm doing bad?
db.places.insert( {
id:1,
name: "AAAAAAAAAAA",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7191 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:2,
name: "BBBBBBBBBBB",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7193 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:3,
name: "CCCCCCCCCCCCC",
locals:[
{
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
}
],
category: "Parks"
} );
//Create index
db.places.createIndex({ "locals.location" : "2dsphere" })
//Query without result
db.places.find(
{
"locals.location":
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
The places has many locals, that's why I can have many objects inside.
I hope some one can help me.
Cheers.
mongodb geolocation azure-cosmosdb geojson 2dsphere
mongodb geolocation azure-cosmosdb geojson 2dsphere
asked Nov 13 '18 at 21:56
Paul EscarcenaPaul Escarcena
82
82
2
You are using CosmosDB? If so then I don't see this working. There is a supported way for MongoDB with the aggregation framework, but this is not compatible with the "compatibility layer" of CosmosDB. These are two completely different products, despite the marketing hype claiming otherwise.
– Neil Lunn
Nov 13 '18 at 22:03
1
See also How to query $near in CosmosDB via mongoDB protocol. As for "an array of sub-documents with location", I would not recommend it. Your example is trivial ( for MongoDB at least ) but even the unsupported$geoNear
can only identify at most one match from the sub-documents. See $geoNear matching nearest array for that one.
– Neil Lunn
Nov 13 '18 at 22:19
Hi,does my answer helps you?
– Jay Gong
Nov 15 '18 at 1:17
Thx @NeilLunn you are right.
– Paul Escarcena
Nov 15 '18 at 14:01
add a comment |
2
You are using CosmosDB? If so then I don't see this working. There is a supported way for MongoDB with the aggregation framework, but this is not compatible with the "compatibility layer" of CosmosDB. These are two completely different products, despite the marketing hype claiming otherwise.
– Neil Lunn
Nov 13 '18 at 22:03
1
See also How to query $near in CosmosDB via mongoDB protocol. As for "an array of sub-documents with location", I would not recommend it. Your example is trivial ( for MongoDB at least ) but even the unsupported$geoNear
can only identify at most one match from the sub-documents. See $geoNear matching nearest array for that one.
– Neil Lunn
Nov 13 '18 at 22:19
Hi,does my answer helps you?
– Jay Gong
Nov 15 '18 at 1:17
Thx @NeilLunn you are right.
– Paul Escarcena
Nov 15 '18 at 14:01
2
2
You are using CosmosDB? If so then I don't see this working. There is a supported way for MongoDB with the aggregation framework, but this is not compatible with the "compatibility layer" of CosmosDB. These are two completely different products, despite the marketing hype claiming otherwise.
– Neil Lunn
Nov 13 '18 at 22:03
You are using CosmosDB? If so then I don't see this working. There is a supported way for MongoDB with the aggregation framework, but this is not compatible with the "compatibility layer" of CosmosDB. These are two completely different products, despite the marketing hype claiming otherwise.
– Neil Lunn
Nov 13 '18 at 22:03
1
1
See also How to query $near in CosmosDB via mongoDB protocol. As for "an array of sub-documents with location", I would not recommend it. Your example is trivial ( for MongoDB at least ) but even the unsupported
$geoNear
can only identify at most one match from the sub-documents. See $geoNear matching nearest array for that one.– Neil Lunn
Nov 13 '18 at 22:19
See also How to query $near in CosmosDB via mongoDB protocol. As for "an array of sub-documents with location", I would not recommend it. Your example is trivial ( for MongoDB at least ) but even the unsupported
$geoNear
can only identify at most one match from the sub-documents. See $geoNear matching nearest array for that one.– Neil Lunn
Nov 13 '18 at 22:19
Hi,does my answer helps you?
– Jay Gong
Nov 15 '18 at 1:17
Hi,does my answer helps you?
– Jay Gong
Nov 15 '18 at 1:17
Thx @NeilLunn you are right.
– Paul Escarcena
Nov 15 '18 at 14:01
Thx @NeilLunn you are right.
– Paul Escarcena
Nov 15 '18 at 14:01
add a comment |
1 Answer
1
active
oldest
votes
Based on my test with your sample data, also got empty result as same as you. Per my knowledge, CosmosDB just supports a subset of the MongoDB API and translates requests into the CosmosDB SQL equivalent. CosmosDB has some different behaviours and results. But the onus is on CosmosDB to improve their emulation of MongoDB.
Certainly, you could add feedback here to submit your requirements or consider using MongoDB Atlas on Azure if you'd like full MongoDB feature support.
Hi, thx for you answer, I´m learning more about CosmosDB and sending an email to azure feedback. I need to find an alternative using any CosmosDB api. Chreers.
– Paul Escarcena
Nov 15 '18 at 14:00
@PaulEscarcenaSantana How about cosmos db sql api? You could use stored procedure or udf to implement your requirements.
– Jay Gong
Nov 16 '18 at 1:26
Thx @Jay. I've tested with Sql Api and work's.
– Paul Escarcena
Nov 16 '18 at 16:22
@PaulEscarcena Sure,you could mark if for answer ,thanks !
– Jay Gong
Nov 17 '18 at 6:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290119%2fmongodb-find-near-in-object-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based on my test with your sample data, also got empty result as same as you. Per my knowledge, CosmosDB just supports a subset of the MongoDB API and translates requests into the CosmosDB SQL equivalent. CosmosDB has some different behaviours and results. But the onus is on CosmosDB to improve their emulation of MongoDB.
Certainly, you could add feedback here to submit your requirements or consider using MongoDB Atlas on Azure if you'd like full MongoDB feature support.
Hi, thx for you answer, I´m learning more about CosmosDB and sending an email to azure feedback. I need to find an alternative using any CosmosDB api. Chreers.
– Paul Escarcena
Nov 15 '18 at 14:00
@PaulEscarcenaSantana How about cosmos db sql api? You could use stored procedure or udf to implement your requirements.
– Jay Gong
Nov 16 '18 at 1:26
Thx @Jay. I've tested with Sql Api and work's.
– Paul Escarcena
Nov 16 '18 at 16:22
@PaulEscarcena Sure,you could mark if for answer ,thanks !
– Jay Gong
Nov 17 '18 at 6:47
add a comment |
Based on my test with your sample data, also got empty result as same as you. Per my knowledge, CosmosDB just supports a subset of the MongoDB API and translates requests into the CosmosDB SQL equivalent. CosmosDB has some different behaviours and results. But the onus is on CosmosDB to improve their emulation of MongoDB.
Certainly, you could add feedback here to submit your requirements or consider using MongoDB Atlas on Azure if you'd like full MongoDB feature support.
Hi, thx for you answer, I´m learning more about CosmosDB and sending an email to azure feedback. I need to find an alternative using any CosmosDB api. Chreers.
– Paul Escarcena
Nov 15 '18 at 14:00
@PaulEscarcenaSantana How about cosmos db sql api? You could use stored procedure or udf to implement your requirements.
– Jay Gong
Nov 16 '18 at 1:26
Thx @Jay. I've tested with Sql Api and work's.
– Paul Escarcena
Nov 16 '18 at 16:22
@PaulEscarcena Sure,you could mark if for answer ,thanks !
– Jay Gong
Nov 17 '18 at 6:47
add a comment |
Based on my test with your sample data, also got empty result as same as you. Per my knowledge, CosmosDB just supports a subset of the MongoDB API and translates requests into the CosmosDB SQL equivalent. CosmosDB has some different behaviours and results. But the onus is on CosmosDB to improve their emulation of MongoDB.
Certainly, you could add feedback here to submit your requirements or consider using MongoDB Atlas on Azure if you'd like full MongoDB feature support.
Based on my test with your sample data, also got empty result as same as you. Per my knowledge, CosmosDB just supports a subset of the MongoDB API and translates requests into the CosmosDB SQL equivalent. CosmosDB has some different behaviours and results. But the onus is on CosmosDB to improve their emulation of MongoDB.
Certainly, you could add feedback here to submit your requirements or consider using MongoDB Atlas on Azure if you'd like full MongoDB feature support.
answered Nov 14 '18 at 1:52
Jay GongJay Gong
8,6001512
8,6001512
Hi, thx for you answer, I´m learning more about CosmosDB and sending an email to azure feedback. I need to find an alternative using any CosmosDB api. Chreers.
– Paul Escarcena
Nov 15 '18 at 14:00
@PaulEscarcenaSantana How about cosmos db sql api? You could use stored procedure or udf to implement your requirements.
– Jay Gong
Nov 16 '18 at 1:26
Thx @Jay. I've tested with Sql Api and work's.
– Paul Escarcena
Nov 16 '18 at 16:22
@PaulEscarcena Sure,you could mark if for answer ,thanks !
– Jay Gong
Nov 17 '18 at 6:47
add a comment |
Hi, thx for you answer, I´m learning more about CosmosDB and sending an email to azure feedback. I need to find an alternative using any CosmosDB api. Chreers.
– Paul Escarcena
Nov 15 '18 at 14:00
@PaulEscarcenaSantana How about cosmos db sql api? You could use stored procedure or udf to implement your requirements.
– Jay Gong
Nov 16 '18 at 1:26
Thx @Jay. I've tested with Sql Api and work's.
– Paul Escarcena
Nov 16 '18 at 16:22
@PaulEscarcena Sure,you could mark if for answer ,thanks !
– Jay Gong
Nov 17 '18 at 6:47
Hi, thx for you answer, I´m learning more about CosmosDB and sending an email to azure feedback. I need to find an alternative using any CosmosDB api. Chreers.
– Paul Escarcena
Nov 15 '18 at 14:00
Hi, thx for you answer, I´m learning more about CosmosDB and sending an email to azure feedback. I need to find an alternative using any CosmosDB api. Chreers.
– Paul Escarcena
Nov 15 '18 at 14:00
@PaulEscarcenaSantana How about cosmos db sql api? You could use stored procedure or udf to implement your requirements.
– Jay Gong
Nov 16 '18 at 1:26
@PaulEscarcenaSantana How about cosmos db sql api? You could use stored procedure or udf to implement your requirements.
– Jay Gong
Nov 16 '18 at 1:26
Thx @Jay. I've tested with Sql Api and work's.
– Paul Escarcena
Nov 16 '18 at 16:22
Thx @Jay. I've tested with Sql Api and work's.
– Paul Escarcena
Nov 16 '18 at 16:22
@PaulEscarcena Sure,you could mark if for answer ,thanks !
– Jay Gong
Nov 17 '18 at 6:47
@PaulEscarcena Sure,you could mark if for answer ,thanks !
– Jay Gong
Nov 17 '18 at 6:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290119%2fmongodb-find-near-in-object-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You are using CosmosDB? If so then I don't see this working. There is a supported way for MongoDB with the aggregation framework, but this is not compatible with the "compatibility layer" of CosmosDB. These are two completely different products, despite the marketing hype claiming otherwise.
– Neil Lunn
Nov 13 '18 at 22:03
1
See also How to query $near in CosmosDB via mongoDB protocol. As for "an array of sub-documents with location", I would not recommend it. Your example is trivial ( for MongoDB at least ) but even the unsupported
$geoNear
can only identify at most one match from the sub-documents. See $geoNear matching nearest array for that one.– Neil Lunn
Nov 13 '18 at 22:19
Hi,does my answer helps you?
– Jay Gong
Nov 15 '18 at 1:17
Thx @NeilLunn you are right.
– Paul Escarcena
Nov 15 '18 at 14:01