How to pass ArrayList as data to Postman - arrays

[ {"roleName" : "admin", "roleIds" : [1,2,3] },
{"roleName" : "editor", "roleIds" : [4,5,6] },
{"roleName" : "contributor", "roleIds" : [7,8,9]} ]
My code Looks like this and the error I am getting is, "Cannot generate variable name for non-typed Collection parameter type"

Related

Laravel Input array of objects to api routes

I'm passing the following request to laravel api, but when I dump the input request, Laravel is returning an empty array.
{
"expense_type" : "payment",
"description" : "test",
"notes" : "My notes",
"expense_date": "2019-01-15",
"cost" : 100.50,
"group_id" : 1,
"shares" : [
{
"user_id" : 1,
"paid_share" : 100.50,
"owed_share" : 00.00
},
{
"user_id" : 2,
"paid_share" : 00.00,
"owed_share" : 50.25
},
{
"user_id" : 3,
"paid_share" : 00.00,
"owed_share" : 50.25
}
]
}
The output when dumping the input $request->all() is []
can some one help if there is something wrong in the request
When you send data in request as json data then $request->all() will always return empty. In order to access data in $request->all() variable you have to send form data not json data.
So, when json data is sent through api, you can access it as array in the controller like this:
$data = json_decode(file_get_contents("php://input"), true);
Now, you can access the values like $data['expense_type']
I hope you understand.

Find element inside array and return only array

I'm new to MongoDB, so i'm having some newbie problems.
I have this document on DB
{
"_id" : ObjectId("5a7ba73dbf27878474ac7682"),
"Enterprise" : "SpaceX",
"Address" : "qwerty",
"users" : [
{
"name" : "Elon",
"number" : "123456",
"email" : "elon#mars.com",
"user" : "elon",
"pass" : "byeroadster"
},
{
"name" : "Larry",
"number" : "3215465",
"email" : "larry#google.com",
"user" : "larry",
"pass" : "googlepassword"
}
]
}
And i'm doing this query
var db = client.db('hauz_manager');
var findUser = db.collection('system_enterprise');
findUser.find({'users.user': data.user},{contatos: 1}).toArray(function(err, result){
console.log(result.users);
});
With that, i'm having like
Enterprise: "SpaceX"
...
users: [{Object}]
And, i'm not able to do some result.users, returns undefined.
tnx.
console.log(result.users)
I think your callback return array.
You should check callback.
Do you need a loop?

String from document meets value of array

I've got an array of Project ID's, for example:
[ 'ExneN3NdwmGPgRj5o', 'hXoRA7moQhqjwtaiY' ]
And in my Questions collection, I've got a field called 'project', which has a string of a project Id. For example:
{
"_id" : "XPRbFupkJPmrmvcin",
"question" : "Vraag 13",
"answer" : "photo",
"project" : "ExneN3NdwmGPgRj5o",
"datetime_from" : ISODate("2017-01-10T08:01:00Z"),
"datetime_till" : ISODate("2017-01-10T19:00:00Z"),
"createdAt" : ISODate("2017-01-10T08:41:39.950Z"),
"notificationSent" : true
}
{
"_id" : "EdFH6bo2xBPht5kYW",
"question" : "sdfadsfasdf",
"answer" : "text",
"project" : "hXoRA7moQhqjwtaiY",
"datetime_from" : ISODate("2017-01-11T11:00:00Z"),
"datetime_till" : ISODate("2017-01-11T17:00:00Z"),
"createdAt" : ISODate("2017-01-10T10:21:42.147Z"),
"notificationSent" : false
}
Now I want to return all documents of the Questions collection, where the Project (id) is one of the value's from the Array.
To test if it's working, I'm first trying to return one document.
Im console.logging like this:
Questions.findOne({project: { $eq: projectArray }})['_id'];
but have also tryed this:
Questions.findOne({project: { $in: [projectArray] }})['_id'];
But keep getting 'undefined'
Please try this.
Questions.find({project: { $in: projectArray }}) => for fetching all docs with those ids
Questions.findOne({project: { $in: projectArray }}) => if you want just one doc

Groovy & json: How to get the length of an array from json?

I am using curl to get some data, then parsing it with JsonSlurper.
The data structure is
"results" : [
{
"uri" : "http://localhost:8081/artifactory/api/storage/...",
"created" : "2015-11-27"
},
{
"uri" : "http://localhost:8081/artifactory/api/storage/...",
"created" : "2015-11-27"
},
{
"uri" : "http://localhost:8081/artifactory/api/storage/...",
"created" : "2015-11-30"
}
]
So if I'm not mistaken, the entire thing is considered an object. But there is an array in the object (results) which contains objects in that array.
I need to get the length of the results array. When I try to do json.result.length, I receive a null.
How can I get the length of the results array?
def list = ['curl', '-u', 'user:pass', "http://localhost:8081/..."].execute()
def json = new JsonSlurper().parseText(list.text)
println json.result.size()
What I see is, you are using incorrect property to get the size. Need to use results instead of result. Thats is the reason you are seeing that error as result is null(because there is no such property)
Here is working script and gets output as 3:
import net.sf.json.groovy.JsonSlurper
def jsonText='''{
"results" : [
{
"uri" : "http://localhost:8081/artifactory/api/storage/...",
"created" : "2015-11-27"
},
{
"uri" : "http://localhost:8081/artifactory/api/storage/...",
"created" : "2015-11-27"
},
{
"uri" : "http://localhost:8081/artifactory/api/storage/...",
"created" : "2015-11-30"
}
]
}'''
def json = new JsonSlurper().parseText(jsonText)
println json.results.size()
assert 3 == json.results.size(), "Array size is not matching"
It will be:
def response = ... // your response as text, stream..
def parsed = new JsonSluper().parse(response) // parseText if string
println parsed.results.size()
BTW: size() is for Collection, length for String, note method vs field. In groovy you can use size() for String as well.
{
"projects":
[
{
"platform": "java",
"name":"abc"
},
{
"platform": ".net",
"name":"abcd"
}]
}
for this json file list.json how to get json size or number of applications count.
below code worked for me in groovy as i was using in it my jenkinsfile.
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.io.FileType
def applicationCount()
{
jsonFile1 = 'list.json'
def json1 = readJSON file: jsonFile1
totalApplication = json1.projects.size()
println "count" + totalApplication
println "applicationname" + json1['projects'][0]['name']
}

How do I search for a string in a MongoDB document array and project the array value in a find operation?

I have created and inserted some simple json documents, each with an array into mongo:
MongoDB shell version: 2.4.6
use test
> db.sandbox.insert({ "array1" : [ "praxis a", "value b", "theory c"] })
> db.sandbox.insert({ "array1" : [ "mean d", "minimum e"] })
> db.sandbox.insert({ "array1" : [ "maximum f"] })
Then I searched for documents in the collection that contained an array1 value starting with the string 'field1'
> db.sandbox.find({"array1" : /praxis/})
This returned a single document, as expected:
{ "_id" : ObjectId("52585223b8a783860470f07b"), "array1" : [ "praxis a", "value b", "theory c" ] }
However, what I really wanted was to only return the array1 field value that matched. So I tried to project the first matching item from the array:
> db.sandbox.find({"array1" : /praxis/}, {_id: 1, "array1.$": 1})
When I tried this, I got a strange error:
error: {
"$err" : "positional operator (array1.$) requires corresponding field in
query specifier",
"code" : 16352
}
What I was hoping to get back was the value:
"field1: a"
What I thought I was settling for was to get back the array:
{ "array1: [ "praxis a", "value b", "theory c"] }
I would appreciate some help with the find function and how to project the value from the array using $ match...
This sure looks like a bug, and is similar to SERVER-9028.
There does seem to be a couple work-arounds, as both of these work:
db.sandbox.find({array1: 'praxis a'}, {'array1.$': 1})
OR
db.sandbox.find({array1: {$in: [/praxis a/]}}, {'array1.$': 1})
Both of these produce output of:
{
"_id": ObjectId("5258620315b3beb195f855b8"),
"array1": [
"praxis a"
]
}

Resources