I have cosmos db items that are structured like this:
{
"id": "ec79ff89-13a2-4da9-9a8d-e0ccc8e55f8e",
"AttachmentIds": [
"aa0805b7-e5f9-42ee-8cb9-07beadaaed31",
"15072c53-3c4b-47c2-ac91-8d9d3138bf96"
],
"_ts": 1589411571
}
I just want to write a simple query that returns the items that have an "AttachmentId" containing "aa0805b7-e5f9-42ee-8cb9-07beadaaed31". I cannot seem to get it to work. This is what I have so far, but I'm getting a syntax error:
SELECT * FROM c where ARRAY_CONTAINS(c.AttachmentIds, {"aa0805b7-e5f9-42ee-8cb9-07beadaaed31"}, true)
Any help is greatly appreciated.
Please change your query to
SELECT * FROM c where ARRAY_CONTAINS(c.AttachmentIds, "aa0805b7-e5f9-42ee-8cb9-07beadaaed31", true)
and that should fix the problem you're having. I simply removed the curly braces from your query as you're trying to find a simple value.
You can find more information about ARRAY_CONTAINS here: https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-array-contains.
Related
This question already has answers here:
How to query MongoDB with "like"
(45 answers)
Closed 2 years ago.
I am trying to query a collection with a like query, which we do in relational DB. I am trying to get a config which ends with LOYALTY_SERVICE_ENABLED
However, I couldn't use like query as I wanted. Can you explain the difference between the two queries below? Thanks
RETURNS 1 RESULT
db.getCollection("configurations").find(
{ key: 'co:food:LOYALTY_SERVICE_ENABLED' }
);
RETURNS 0 RESULT
db.getCollection("configurations").find(
{ key: '/.*LOYALTY_SERVICE_ENABLED' }
);
Equal SQL Query:Select * from configurations where key like '%LOYALTY_SERVICE_ENABLED';
It looks like a typo. can you try it like this?
db.getCollection(“configurations”).find({"key" : /.*LOYALTY_SERVICE_ENABLED.*/}).pretty();
You will need to use the $regex evaluation query operator to use a pattern matching string, I have modified your code to reflect that.
db.getCollection("configurations").find(
{ key: {$regex: /.*LOYALTY_SERVICE_ENABLED/} }
);
Notice you don't need the quotes ' ' around the pattern string.
More details can be found here
I am using Scala 2.12 to query the ElasticSearch (6.5).
I am able to use querybuilders for a single field search like below:
val searchSourceBuilder = new SearchSourceBuilder()
val qb = new BoolQueryBuilder()
.must(QueryBuilders.regexpQuery("header.fieldname", "01_.+_20190711_data"))
searchSourceBuilder.query(qb)
Using the above (I need regex search) I can search the relevant documents.
However, I have more complex requirement, where I have to match the documents on more than one field-value pair.
i.e.
header.fieldname should match pattern "01_.+data"
AND
header.fieldname2 should match pattern "type.+_2019-07-11"
Basically, it is like SQL where clause on 2 or more columns (and value string).
I was checking https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
But this is like searching the same string (value) in multiple fields. This is NOT what I want.
I basically want something like SQL AND in where clause (better if it is with regex too).
UPDATE:
Please note the below answer by #Meet Rathod works and accepted.
However, to take it forward, so if I need one more condition which is SQL OR, is my below code correct.
Required:
header.fieldname: 01_.+data AND header.fieldname2: type.+_2019-07-11 AND (header.fieldname3: some_thing OR header.fieldname3: some_other_thing)
Code:
val qb = new BoolQueryBuilder()
.must(QueryBuilders.regexpQuery("header.fieldname", "01_.+_20190711_data"))
.must(QueryBuilders.regexpQuery("header.fieldname2", "type.+_2019-07-11"))
.should(QueryBuilders.regexpQuery("header.fieldname3", "some_thing"))
.should(QueryBuilders.regexpQuery("header.fieldname3", "some_other_thing"))
Is this correct or I am missing something?
As far as I understand, you want only those document which satisfies all your conditions should list out in the result. And if that's the case I believe adding another must clause in your query should get your expected result. The raw query will look something like this.
{
"query": {
"bool": {
"must": [
{
"regexp": {
"header.fieldname": "01_.+data"
}
},
{
"regexp": {
"header.fieldname2": "type.+_2019-07-11"
}
}
]
}
}
}
I'm not sure but, your Scala code should look something like this.
val qb = new BoolQueryBuilder()
.must(QueryBuilders.regexpQuery("header.fieldname", "01_.+_20190711_data"))
.must(QueryBuilders.regexpQuery("header.fieldname2", "type.+_2019-07-11"))
I've found this question answered for C# and Perl, but not in the native interface. I thought this would work:
db.theColl.find( { _id: ObjectId("4ecbe7f9e8c1c9092c000027") } )
The query returned no results. I found the 4ecbe7f9e8c1c9092c000027 by doing db.theColl.find() and grabbing an ObjectId. There are several thousand objects in that collection.
I've read all the pages that I could find on the mongodb.org website and didn't find it. Is this just a strange thing to do? It seems pretty normal to me.
Not strange at all, people do this all the time. Make sure the collection name is correct (case matters) and that the ObjectId is exact.
Documentation is here
> db.test.insert({x: 1})
> db.test.find() // no criteria
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }
> db.test.find({"_id" : ObjectId("4ecc05e55dd98a436ddcc47c")}) // explicit
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }
> db.test.find(ObjectId("4ecc05e55dd98a436ddcc47c")) // shortcut
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }
If you're using Node.js:
var ObjectId = require('mongodb').ObjectId;
var id = req.params.gonderi_id;
var o_id = new ObjectId(id);
db.test.find({_id:o_id})
Edit: corrected to new ObjectId(id), not new ObjectID(id)
Even easier, especially with tab completion:
db.test.find(ObjectId('4ecc05e55dd98a436ddcc47c'))
Edit: also works with the findOne command for prettier output.
You Have missed to insert Double Quotes.
The Exact Query is
db.theColl.find( { "_id": ObjectId("4ecbe7f9e8c1c9092c000027") } )
If you are working on the mongo shell, Please refer this : Answer from Tyler Brock
I wrote the answer if you are using mongodb using node.js
You don't need to convert the id into an ObjectId. Just use :
db.collection.findById('4ecbe7f9e8c1c9092c000027');
this collection method will automatically convert id into ObjectId.
On the other hand :
db.collection.findOne({"_id":'4ecbe7f9e8c1c9092c000027'}) doesn't work as expected. You've manually convert id into ObjectId.
That can be done like this :
let id = '58c85d1b7932a14c7a0a320d';
let o_id = new ObjectId(id); // id as a string is passed
db.collection.findOne({"_id":o_id});
I think you better write something like this:
db.getCollection('Blog').find({"_id":ObjectId("58f6724e97990e9de4f17c23")})
Once you opened the mongo CLI, connected and authorized on the right database.
The following example shows how to find the document with the _id=568c28fffc4be30d44d0398e from a collection called “products”:
db.products.find({"_id": ObjectId("568c28fffc4be30d44d0398e")})
I just had this issue and was doing exactly as was documented and it still was not working.
Look at your error message and make sure you do not have any special characters copied in. I was getting the error
SyntaxError: illegal character #(shell):1:43
When I went to character 43 it was just the start of my object ID, after the open quotes, exactly as I pasted it in. I put my cursor there and hit backspace nothing appeared to happen when it should have removed the open quote. I hit backspace again and it removed the open quote, then I put the quote back in and executed the query and it worked, despite looking exactly the same.
I was doing development in WebMatrix and copied the object id from the console. Whenever you copy from the console in WebMatrix you're likely to pick up some invisible characters that will cause errors.
In MongoDB Stitch functions it can be done using BSON like below:
Use the ObjectId helper in the BSON utility package for this purpose like in the follwing example:
var id = "5bb9e9f84186b222c8901149";
BSON.ObjectId(id);
For Pythonists:
import pymongo
from bson.objectid import ObjectId
...
for row in collectionName.find(
{"_id" : ObjectId("63ae807ec4270c7a0b0f2c4f")}):
print(row)
To use Objectid method you don't need to import it. It is already on the mongodb object.
var ObjectId = new db.ObjectId('58c85d1b7932a14c7a0a320d');
db.yourCollection.findOne({ _id: ObjectId }, function (err, info) {
console.log(info)
});
Simply do:
db.getCollection('test').find('4ecbe7f9e8c1c9092c000027');
I have a question pertaining to how to query in cloudant with (1) a nested array and (2) sort by using an if-statement. I have been to the following websites, but I still need assistance.
https://docs.cloudant.com/guides/cloudant-query.html
https://docs.cloudant.com/api/cloudant-query.html
I want to return the documents that meet satisfy the following pseudo code:
if event[i].type == "check_in" then sort by event[i].time
Here is a snippet of the JSON data structure that I am using.
{
"status" : "active",
"event": [
{
"type": "check_in",
"time": "11/19/2014 15:34:12"
},
{
"type": "check_out",
"time": "11/20/2014 17:54:22"
}
]
}
Here are some questions I have that may break this problem down:
(1) How can I access event[0].type data?
(2) How can I loop through the entire event array inside of a Cloudant Query and check if event[i] == "check_in" for each object in the event array?
(3) How can I sort on the timestamp data (assume it is an integer for simplicity)?
(4) What format does the timestamp have to be in for me to sort it in a Cloudant Query?
Could you help point me in the right direction to help accomplish this? Please let me know if you need more information.
Cloudant Query doesn't seem to work with indexing array elements and query against.
You can create a View to get the same result by indexing each array elements and then query on the View.
My goal is to copy every item in collection 1 to collection 2 in order of "page_length" descending, such that the new collection has the largest page_length field first, second is the second largest and so on.
I tried the following in Mongo:
db.mycollection1.find().sort({page_length:-1}).foreach(function(d) { db.mycollection2.insert({"field1":d.field1, "field2":d.field2}); })
Got an error that DBQuery has no method 'foreach'. I know that foreach() is a function of find() but I was hoping to find a workaround. I even tried the following:
db.mycollection1.aggregate({$sort:{page_length:-1}}).foreach(function(d) { db.mycollection2.insert({"field1":d.field1, "field2":d.field2}); })
Any ideas?
It looks like your first example is correct except that you have foreach instead of forEach. It needs the upper case E.
http://docs.mongodb.org/manual/reference/method/cursor.forEach/
Try:
db.mycollection1.find().sort().forEach(function(e) { ... })
You need to sort first here I have sort with Last Name of Customer try below query
db.customers.find().sort({lastname:1}).forEach(function(doc){print("Customer name:"+doc.lastname)});