You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为字段添加数组而非字符串类型值

Adding a Numerical Array to Your Field (Instead of a String)

Hey there! Adding that numerical array to your field instead of a string is totally doable—let's cover the most common scenarios based on what tech stack you're working with:

1. JavaScript/Node.js (e.g., MongoDB/Mongoose Backend)

If you're using Mongoose to define your database schema, make sure your field is explicitly typed as a number array, then assign your raw numerical array directly (no string conversion!):

// Define your schema with a number array field
const yourSchema = new mongoose.Schema({
  targetField: {
    type: [Number], // Critical: sets the field to accept an array of numbers
    required: true
  }
});

// Create and save a document with your array
const yourDocument = new YourModel({
  targetField: [0,16,33,50,67,84,101,118,135,152,169,186,203,220,237,254,271,288,305,322,338,355,372,389,406,423,440,457,474,491,508,525,542,559,576,593,610,627,644,661,677,694,711,728,745,762,779,796,813,830,847,864,881,898,915,932,949,966,983,1000,1016,1033,1050,1067,1084,1101,1118,1135,1152,1169,1186,1203,1220,1237,1254,1271,1288,1305,1322,1338,1355,1372,1389,1406,1423,1440,1457,1474,1491,1508,1525,1542,1559,1576,1593,1610,1627,...]
});

await yourDocument.save();

The key here is avoiding any JSON.stringify() calls on the array—pass it as a native JavaScript array, and Mongoose will handle storing it correctly as a numerical array in MongoDB.

2. Python/Django (Model Field)

For Django, if you're using PostgreSQL, you can use ArrayField with an IntegerField base type. If you're using another database, JSONField works too (just make sure to store a native integer array, not a string):

Option 1: PostgreSQL ArrayField

from django.db import models
from django.contrib.postgres.fields import ArrayField

class YourModel(models.Model):
    target_field = ArrayField(
        models.IntegerField(),
        default=list, # Optional: sets empty array as default
        blank=True
    )

# Assign and save your array
obj = YourModel()
obj.target_field = [0,16,33,50,67,84,101,118,135,152,169,186,203,220,237,254,271,288,305,322,338,355,372,389,406,423,440,457,474,491,508,525,542,559,576,593,610,627,644,661,677,694,711,728,745,762,779,796,813,830,847,864,881,898,915,932,949,966,983,1000,1016,1033,1050,1067,1084,1101,1118,1135,1152,1169,1186,1203,1220,1237,1254,1271,1288,1305,1322,1338,1355,1372,1389,1406,1423,1440,1457,1474,1491,1508,1525,1542,1559,1576,1593,1610,1627,...]
obj.save()

Option 2: JSONField (Works with any database)

from django.db import models

class YourModel(models.Model):
    target_field = models.JSONField(default=list)

# Same assignment as above
obj.target_field = [0,16,33,...]
obj.save()

3. Frontend (e.g., React Form Submission)

If you're sending the array from a frontend to a backend, make sure you're sending it as a JSON array (not a stringified version) in your request body:

// Prepare your data as a native array
const submissionData = {
  targetField: [0,16,33,50,67,...]
};

// Send the request with JSON content type
fetch('/api/your-endpoint', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(submissionData), // Stringify the entire object, not just the array
});

On the backend, ensure you're parsing the JSON request body correctly so the array stays as a numerical array, not a string.

Quick Fix for Existing String Arrays

If you accidentally stored the array as a string before, you can convert it back to a numerical array easily:

  • In JavaScript:
    const stringifiedArray = "[0,16,33,...]";
    const numericalArray = JSON.parse(stringifiedArray).map(Number);
    
  • In Python:
    import json
    stringified_array = "[0,16,33,...]"
    numerical_array = list(map(int, json.loads(stringified_array)))
    

内容的提问来源于stack exchange,提问作者André Nakazato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:57:53