i am using mongo DB 3.0 version and restheart API 2.0 version. now i am trying to check my queries (URI) through postman chrome interface. and i create a database(test) table(mycol) and two documents in mongo DB, when i filter that data it shows correctly but when i try to insert data into mongodb through postman or HAL Browser it shows error, can you peoples please guide me the syntax format.
Query for filter data, it gives correct result
Query for insert a document, it shows some error
and also i need to know <docid> in the URI format : /<dbname>/<collname>/<docid>[?doc_type=TYPE] what it means <docid> please explain in detail with some example
To create a document you need either to POST the collection POST /test/mycol or PUT a document PUT /test/mycol/<docid>
<docid> stands for document id. the query parameterdocid_type is optional and allows to specify the type of the <docid> in the URL, more information in the documentation Resource URI section.
For instance, if you want to create the following document { "_id": “mydoc", “message”: “hello” } you do
PUT /test/mycol/mydoc { “message”: “hello”}
or
POST /test/mycol { "_id": “mydoc", “message”: “hello” }
In the latter case, if you don’t specify the _id, it will be autogenerated as an ObjectId.
Note that you have to specify the Content-Type request header to be either application/json or application/hal+json.
For instance, using Postman you set the body to be raw and select JSON (application/json) from the dropdown on the right. You'll notice that this will add the Content-Type header to the headers.
I run into the same problem.
The problem was I used field names starting with "_"
i.e. field names like "_type", "_name".
Try to avoid such names.
I had the same problems as you. For insert you should just write your object like this:
{
"code": 20,
"name": "s",
"family": "x"
}
And set POST your method, also for update if the document exists it will be updated, otherwise it will be created.
Please look at this link for more information
https://community.boomi.com/s/article/howtointegratewithmongodbusingopensourcerestheartlibrary#jive_content_id_Scenario_1__InsertUpdate_an_Employee_record_in_Employees_collection_using_POST
Related
I have this URL string:
http://localhost:9033/api/v1/myapi/account/123456/collection/COLL12345
I want to amend it to also include document ID's. There could be one or more documents so I think these should be in some form of array. Should I change my API call from GET to POST and include the document ID's as a JSON array in the message body or is it possible to construct a URL string with them?
You can both add them to the URI path or the URI query. I would use the query for this, something like: /api/v1/myapi/account/123456/collection/COLL12345?ids=[1,4,5]. You need to URI encode the [1,4,5] part and URI decode and JSON parse it on the server.
I'm using MongoDB Stitch to create a data enabled API, but when I make a GET request, the data is returned where numbers are displayed as:
"firstHit": {
"$numberInt": "3"
Where I would like them to be return just as:
"firstHit": 3
I have a lot of objects within objects, and I am inserting the data through the mongo shell, I'm not sure of that is of any importance.
Anyone have any experience with this? Thank you!
By default, the result format returned by MongoDB Stitch webhooks is in MongoDB Extended JSON format, or EJSON for short. This is useful to define data types that would otherwise be lost in normal JSON. There are some object types that have no equivalent in JSON, for example ObjectId() and Date().
If you would like to return as a normal JSON, you could set the response object as an example below:
exports = function(payload, response) {
result = {"firsthit": 10};
response.setStatusCode(200);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify(result));
}
You may also find EJSON library and Stitch Utility Packages as useful additional information.
I am trying create document in an existing bucket in CouchBase using N1QL query. I am using soapUI to send request to localhost CouchBase server.
I used this link to gain knowledge
http://developer.couchbase.com/documentation/server/4.5/n1ql/n1ql-rest-api/index.html
N1QL:
GET http://localhost:8093/query/service?statement = ?select * from beer use keys ["beername::abcc"]
this works well to fetch doc
How can I do the same to post json data, creating new document.
I tried:
POST http://localhost:8093/query/service.
In request body
INSERT INTO `beer` ( KEY, VALUE )
VALUES
(
"k001",
{ "id": "01", "type": "airline"}
)
RETURNING META().id as docid, *;
I am getting
"Error processing json request"
From the N1QL documentation:
For POST requests, you can specify the parameters in the request body
in URL-encoded format or JSON format.
So seems that you can't directly specify the statement in the request body, you've to put in URL-encoded format or as JSON like { "statement" : "select ..." }.
Just put the INSERT statment directly in the body of the request:
curl -v http://localhost:8093/query/service -d 'statement=INSERT INTO `beer` ( KEY, VALUE ) VALUES ( "k002", { "id": "01", "type": "airline"} ) RETURNING META().id as docid, *;'
Or in postman http client:
Also, see the latest docs as well. http://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/insert.html
This is such a weird problem. I think it has to do with how I am querying the document. It seems like the Meteor API has changed to query documents but the docs on the website are the same.
Here is a document in the database:
meteor:PRIMARY> db.studies.findOne()
{ "_id" : ObjectId("56c12e6537014a66b16771e7"), "name" : "Study 1" }
I have subscribed to get all documents and here is what I am trying in the console to get the documents.
var study = Studies.findOne() // This works.
It returns:
_id: MongoID.ObjectID
_str: "56c12e6537014a66b16771e7"
name: 'Study 1'
I just started a new Meteor project with React. I see that my collection is returning _id: MongoId.ObjectId
This is different, I have been using Meteor for awhile with Blaze and I can't remember it returning MongoID.ObjectID instead of just the string
But now if I try and find just that one document, it does not work.
var study = Studies.findOne("56c12e6537014a66b16771e7");
or
var study = Studies.findOne({_id: "56c12e6537014a66b16771e7"});
I am positive I am queuing for the right _id field. I have double checked the ID. Why does trying to find this one document not work?
Please let me know how I can query for a document. Has something changed with Meteor? The documentation still says you can search by id string.
You need to explicitly cast object id string to an ObjectID
var study = Studies.findOne({_id: new Meteor.Collection.ObjectID("56c12e6537014a66b16771e7")});
#Jaco has the correct answer, but I wanted to answer here to clarify what the higher level issue was.
The reason why my find query was not following syntax in Meteor docs is because I inserted the document into MongoDB directly, instead of through the Meteor API.
If you insert the document directly into MongoDB, you have to query the document using the syntax #Jaco mentioned in his answer.
Similar question: Meteor - Find a document from collection via Mongo ObjectId
So instead of changing my query code, I just deleted the document I inserted directly into MongoDB, and inserted a documented using the console in the browser.
Now I can query the document like normal.
So the root of the issue is that if you insert the document directly into MongoDB, you don't get the same type of document as you would if you insert the document using the Meteor API.
I put some data to CouchBase 1.8.1,and get it successful.But I want to query its metadata,as expiration and att_reason(non-json or json).In some document,it list the metadata with json format,for example:
{
"_id" : "contact_475",
"_rev" : "1-AB9087AD0977F089",
"_bin" : "...",
"$flags" : 0,
"$expiration" : 0,
"name" : "Fred Bloggs",
}
How can I query item's metadata?
As Pavel the most common way to access metadata in Couchbase (2.0) is using Views.
You can also use the internal TAP protocol :
http://www.couchbase.com/wiki/display/couchbase/TAP+Protocol
Could you give us more information about your use case and why you need to access meta/expiration ? And why you cannot use views (that is the recommended way to do it)
Regards
Tug
The easiest way is issuing an HTTP request to:
http://serveraddress:8091/couchBase/default/contact_475
The response should contain an X-Couchbase-Meta header with the metadata. More information is here: http://xmeblog.blogspot.co.il/2013/08/couchbase-how-to-retrieve-key.html
If you want to see the meta data in a Couchbase query you can do something like this:
SELECT meta(b).* FROM bucket b
You can also see both meta data and all other data in a query by doing something like this:
SELECT meta(b).*, * FROM bucket b
If you want to query only meta data using N1QL you could run below query, it will return all meta data about document :
select meta(bucket_name) from bucket_name
But if you want to get these information from Sync Gateway it will return to you by every GET request using REST API, the REST API also include some filtering above these meta data.